Script to Get free space on C:

I want to get free space on C: on a number of machines listed in a file.
Then export to excel.

Welcome to the forum. :wave:t3:

That’s a nice little task to get easily done with PowerShell. What’s your question? :wink: :slightly_smiling_face:

Please keep in mind: This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Regardless of that:
For the vast majority of the cases you’re not the very first one with a given task. And the task you want to get done is a very basic one. It has been asked and answered many times. So it is not necessary to re-invent the wheel again. Use your prefered internet search engine to search for code examples you can adapt to your particular needs.

If you get stuck at any point you’re very welcome to come back, share your code and ask a specific question about the code.

Have a very nice day. :love_you_gesture:t3:

1 Like

Thank you so much for your prompt reply. I am completely agreed with you. I’ve written the code but facing some issue. Let me explain:

$computers = get-content “C:\temp\xyz.txt”
for each ($computer in $computers)
{$disk = Get-Ciminstance win32_logicaldisk -computername $computers -filter drivetype=3 | select-object device id,
@{‘name’=‘size’; ‘expression’={[math]::truncate($.size / 1gb)}}
@{‘name’=‘freespace’; ‘expression’={[math]::truncate($
.freespace / 1gb)}}
foreach($disk in $disk)
{$disk.deviceid + $disk.freespace.tostring(“n0”) + "gb / " +$disk.size.tostring(“N0”) + “gb”}

it seems something wrong in this coz its going into loop and not stopping

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Don’t you get error messages when you run this code?

?? What’s that? :smirk: :wink:

In your loop header you create the loop variable $computer but inside your loop you use the list of computers you have in $computers.
I’d recommend NOT to use the plural “s” for arrays / lists. Instead you should use something more obvious like $computerList or $computerArray or $computerCollection or …

And BTW: Actually you don’t need a loop at all. The parameter -ComputerName of the cmdlet Get-CimInstance accepts a list of computer names. :wink: