Select -Object Value

Hi All,

I am pretty new to powershell. I have little doubt in my script .

get-WmiObject win32_logicaldisk -ComputerName computername | Select-object DeviceId,@{n=“Size”;e={[math]::Round($.Size/1GB,2)}},@{n=“FreeSpace”;e={[math]::Round($.FreeSpace/1GB,2)}}

The output

DeviceiD : A:
Size : 0
FreeSpace : 0

DeviceiD : C:
Size : 50
FreeSpace : 6.28

DeviceiD : D:
Size : 50
FreeSpace : 41.06

DeviceiD : E:
Size : 10
FreeSpace : 9.88

DeviceiD : F:
Size : 250
FreeSpace : 249.85

DeviceiD : G:
Size : 80
FreeSpace : 79.9

DeviceiD : H:
Size : 50
FreeSpace : 39.39

DeviceiD : Z:
Size : 0
FreeSpace : 0

Question;

How can I Exclude A and Z Drive in my command mentioned above

Try this…

[pre]
Get-CimInstance -ClassName CIM_LogicalDisk -ComputerName localhost | `
Where-Object DeviceID -NotIn @(‘A:’,‘Z:’) | `
Select-object DeviceId,@{n=“Size”;e={[math]::Round($.Size/1GB,2)}},@{n=“FreeSpace”;e={[math]::Round($.FreeSpace/1GB,2)}}
[/pre]

The question really is what is it you are trying to filter. WMI classes contain many properties, so normally you want to start by just seeing what you CAN filter on:

PS C:\WINDOWS\system32> Get-CimInstance -ClassName Win32_LogicalDisk -Property *



Status                       : 
Availability                 : 
DeviceID                     : C:
StatusInfo                   : 
Caption                      : C:
Description                  : Local Fixed Disk
InstallDate                  : 
Name                         : C:
ConfigManagerErrorCode       : 
ConfigManagerUserConfig      : 
CreationClassName            : Win32_LogicalDisk
ErrorCleared                 : 
ErrorDescription             : 
LastErrorCode                : 
PNPDeviceID                  : 
PowerManagementCapabilities  : 
PowerManagementSupported     : 
SystemCreationClassName      : Win32_ComputerSystem
SystemName                   : DESKTOP-MGT9HIB
Access                       : 0
BlockSize                    : 
ErrorMethodology             : 
NumberOfBlocks               : 
Purpose                      : 
FreeSpace                    : 305088126976
Size                         : 510770802688
Compressed                   : False
DriveType                    : 3
FileSystem                   : NTFS
MaximumComponentLength       : 255
MediaType                    : 12
ProviderName                 : 
QuotasDisabled               : 
QuotasIncomplete             : 
QuotasRebuilding             : 
SupportsDiskQuotas           : False
SupportsFileBasedCompression : True
VolumeDirty                  : 
VolumeName                   : Windows-SSD
VolumeSerialNumber           : 9ABBC3B8
PSComputerName               : 
CimClass                     : root/cimv2:Win32_LogicalDisk
CimInstanceProperties        : {Caption, Description, InstallDate, Name...}
CimSystemProperties          : Microsoft.Management.Infrastructure.CimSystemProperties

For disks, normally folks start with DriveType

DriveType
Data type: uint32
Access type: Read-only
Qualifiers: MappingStrings (“Win32API|FileFunctions|GetDriveType”)
Numeric value that corresponds to the type of disk drive this logical disk represents.

Unknown (0)
No Root Directory (1)
Removable Disk (2)
Local Disk (3)
Network Drive (4)
Compact Disc (5)
RAM Disk (6)

First, you should use Get-CimInstance versus Get-WmiObject, which will at some point be deprecated. Try to filter left, which means you want to filter as soon as possible:

Get-CimInstance -ClassName Win32_LogicalDisk -Property * -Filter 'DriveType = 3'

#vs

Get-CimInstance -ClassName Win32_LogicalDisk -Property * | Where{$_.DriveType -eq 3}

While both work, in the second command you are getting everything and then filtering vs only getting what you need in the first place. If DriveType doesn’t work, you could also filter on Size:

Get-CimInstance -ClassName Win32_LogicalDisk -Property * -Filter 'Size > 0'

Just keep in mind that filtering in WMI uses WQL for filtering, which is similar to SQL.

[quote quote=193742][/quote]
Completely makes sense

Thanks Kiran ,

You Got it