-1

How to get a particular value in PowerShell display ?

Example - When I execute below script I get 6 values i need to get only 4th row value.

Command:

Get-WmiObject win32_logicaldisk -Filter "Drivetype=3

Output:

DeviceID     : C:
DriveType    : 3
ProviderName :
FreeSpace    : 183760687104
Size         : 255791026176
VolumeName   :

I need to fetch only "183760687104" value. How to achieve it. Also I do not want to have FreeSpace tag also. Just plain value "183760687104".

1
  • Those are properties of the object. You should show those you need. Your question is answered here. Commented Dec 18, 2017 at 9:02

1 Answer 1

2

You just access the field by name:

(Get-WmiObject Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace

Note that Get-WmiObject is deprecated, you should be using Get-CimInstance instead.

(Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace

If you want to save the value in a variable, just assign it:

$freespace = (Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace

and then you can use $freespace as you wish. Be careful though as on a system with more than one local disk your expression will return an array of values rather than just a single one, so you may want to subscript the result to choose only the first disk. Either of these expressions will be sure to give you a single number:

PS C:\Users\User> (Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace[0]
94229651456
PS C:\Users\User> (Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3")[0].FreeSpace
94239125504

If you are ever in doubt about the accessible fields on an object, just pipe the expression through gm (short for Get-Member):

Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3" | gm

will list all available fields.

PS C:\Users\User> Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3" | gm


   TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_LogicalDisk

Name                         MemberType  Definition
----                         ----------  ----------
Clone                        Method      System.Object ICloneable.Clone()
Dispose                      Method      void Dispose(), void IDisposable.Dispose()
Equals                       Method      bool Equals(System.Object obj)
GetCimSessionComputerName    Method      string GetCimSessionComputerName()
GetCimSessionInstanceId      Method      guid GetCimSessionInstanceId()
GetHashCode                  Method      int GetHashCode()
GetObjectData                Method      void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Sys...
GetType                      Method      type GetType()
ToString                     Method      string ToString()
Access                       Property    uint16 Access {get;}
Availability                 Property    uint16 Availability {get;}
BlockSize                    Property    uint64 BlockSize {get;}
Caption                      Property    string Caption {get;}
Compressed                   Property    bool Compressed {get;}
ConfigManagerErrorCode       Property    uint32 ConfigManagerErrorCode {get;}
ConfigManagerUserConfig      Property    bool ConfigManagerUserConfig {get;}
CreationClassName            Property    string CreationClassName {get;}
Description                  Property    string Description {get;}
DeviceID                     Property    string DeviceID {get;}
DriveType                    Property    uint32 DriveType {get;}
ErrorCleared                 Property    bool ErrorCleared {get;}
ErrorDescription             Property    string ErrorDescription {get;}
ErrorMethodology             Property    string ErrorMethodology {get;}
FileSystem                   Property    string FileSystem {get;}
FreeSpace                    Property    uint64 FreeSpace {get;}
InstallDate                  Property    CimInstance#DateTime InstallDate {get;}
LastErrorCode                Property    uint32 LastErrorCode {get;}
MaximumComponentLength       Property    uint32 MaximumComponentLength {get;}
MediaType                    Property    uint32 MediaType {get;}
Name                         Property    string Name {get;}
NumberOfBlocks               Property    uint64 NumberOfBlocks {get;set;}
PNPDeviceID                  Property    string PNPDeviceID {get;}
PowerManagementCapabilities  Property    uint16[] PowerManagementCapabilities {get;}
PowerManagementSupported     Property    bool PowerManagementSupported {get;}
ProviderName                 Property    string ProviderName {get;}
PSComputerName               Property    string PSComputerName {get;}
Purpose                      Property    string Purpose {get;}
QuotasDisabled               Property    bool QuotasDisabled {get;}
QuotasIncomplete             Property    bool QuotasIncomplete {get;}
QuotasRebuilding             Property    bool QuotasRebuilding {get;}
Size                         Property    uint64 Size {get;}
Status                       Property    string Status {get;}
StatusInfo                   Property    uint16 StatusInfo {get;}
SupportsDiskQuotas           Property    bool SupportsDiskQuotas {get;}
SupportsFileBasedCompression Property    bool SupportsFileBasedCompression {get;}
SystemCreationClassName      Property    string SystemCreationClassName {get;}
SystemName                   Property    string SystemName {get;}
VolumeDirty                  Property    bool VolumeDirty {get;}
VolumeName                   Property    string VolumeName {get;set;}
VolumeSerialNumber           Property    string VolumeSerialNumber {get;}
PSStatus                     PropertySet PSStatus {Status, Availability, DeviceID, StatusInfo}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.