Add Custom Variable to Select-Object


im trying to get the free Disk Size of 20 Servers into a .csv.

I found the following script but the hostname is missing as a row. How can i add the computer variable into the select-object or my .csv?

foreach($computer in (Get-Content C:\Temp\computerlist.txt)){
Get-WmiObject Win32_LogicalDisk -ComputerName $computer -Filter DriveType=3 |Where-Object Deviceid -eq c: | Select-Object $computer,DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}




Bon, welcome to Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “CODE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

You should not use Get-WMIObject anymore. User Get-CimInstance instead. And you don’t need a loop. Get-CimInstance is able to take an array of computer names.

$ComputerNameList = Get-Content C:\Temp\computerlist.txt
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $ComputerNameList -Filter DriveType=3 |
Where-Object Deviceid -eq c: |
Select-Object PSComputerName, DeviceID,
@{Name = 'Size (GB)'; Expression = { [math]::truncate($_.size / 1GB) } },
@{Name = 'Freespace (GB)'; Expression = { [math]::truncate($_.freespace / 1GB) } }

The forum software seems a little buggy. So please remove the “crayon … inline = true” stuff and replace the “& # 0 3 9 ;” with single quotes. :wink:

Hi Olaf,

thx for the code. I edited my post.
Unfortunately i get an Error :frowning:
Missing Argument for the Parameter “ClassName”

I tryed to change add the classname CIM_LogicalDisk but then i get an error “Get-CimInstance : The WS-Management service cannot process the request. The WQL query is invalid.”

Oooops … my fault … sorry for that … I changed the code above. Simply use “Win32_LogicalDisk” as the class name.

aaaaand it´s working.
Cool :slight_smile:
Thx Olaf