Select -Object Value

Welcome Forums General PowerShell Q&A Select -Object Value

Viewing 4 reply threads
  • Author
    Posts
    • #193685
      Participant
      Topics: 3
      Replies: 2
      Points: 20
      Rank: Member

      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

    • #193706
      Moderator
      Topics: 3
      Replies: 167
      Points: 878
      Helping Hand
      Rank: Major Contributor

      Try this...

      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)}}
      
    • #193742
      Participant
      Topics: 10
      Replies: 1375
      Points: 1,481
      Helping Hand
      Rank: Community Hero

      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.

    • #193751
      Moderator
      Topics: 3
      Replies: 167
      Points: 878
      Helping Hand
      Rank: Major Contributor

      Completely makes sense

    • #194009
      Participant
      Topics: 3
      Replies: 2
      Points: 20
      Rank: Member

      QUOTE

      Thanks Kiran ,

      You Got it

Viewing 4 reply threads
  • You must be logged in to reply to this topic.