header image

COM pt 6–Collections pt 1

Posted by: | February 1, 2011 | No Comment |

If we return to our filesystemobject

$fso = New-Object -ComObject "Scripting.FileSystemObject"

and look at the drives collection



we will see a collection of drives. If we try to index into it



we get error that says
Unable to index into an object of type System.__ComObject.


can do something like this

$drives = $fso.Drives
foreach ($drive in $drives){$drive}


but still doesn’t allow us to reach individual drives

This is an issue with COM collections accessed in PowerShell. They do have a property called Item that we can use. Normally we would do something like



but we get an error about indexing.  If we look at the Item property

PS> $fso.Drives.Item

IsSettable          : False
IsGettable          : True
OverloadDefinitions : {IDrive Item (Variant)}
TypeNameOfValue     : System.Object
MemberType          : ParameterizedProperty
Value               : IDrive Item (Variant) {get}
Name                : Item
IsInstance          : True


It doesn’t want to take a number as an argument. It does take a drive letter

PS> $fso.Drives.Item("c")

Path           : C:
DriveLetter    : C
ShareName      :
DriveType      : 2
RootFolder     : System.__ComObject
AvailableSpace : 191890976768
FreeSpace      : 191890976768
TotalSize      : 249951154176
VolumeName     :
FileSystem     : NTFS
SerialNumber   : 941000572
IsReady        : True


If you know the drives then picking a letter is OK – otherwise we need to display everything as a first pass so we can work out what drives are mapped

under: COM, PowerShellV2