How to remotely pull hard drive details and export to CSV
Updated: Mar 17, 2019
On the Microsoft Technet PowerShell Forums, I found a request to create a PowerShell script to get disk space information from each of the drives on the server. Any drive that is not present on the system he wants "N/A" .
Let's start with a PowerShell command to pull disk information. Looking around I found the get-psdrive command. It has everything the requestor wants Used and Free space and each of the drives. The only issue is he wants only the hard drives.variable
Let's clean up our query by only showing drives with a FileSystem. Much better, but the D drive is a CD Rom drive and not a hard drive.
I was able to find a command that will show only CD Rom drives. Here is the command.
We just need the drive letter so lets do some clean up on the command.
We are slowly making some progress, we now know how to get a list of all hard drives (plus cd rom), and we also know what drive is a cd rom drive. We need to remove the drives that are cd rom drives from our query. I create a foreach loop to filter out the D drive.
We now know the drives we want to pull the information from, we can just create a foreach look with a switch to write to a variable. It took me awhile to figure this part out but I got it working.
At the bottom of the example I just show the output of just the c drive.
Now we need to create a foreach loop for the missing drives and mark them as "N/A". The way I did this was, we know that the hard drive letters can be A-Z and we know what drives are hard drives on the system. So we can remove all the found hard drives from our list of possible hard drive letters.
As you can see from the output of $AllMissingDrives drive letters C,D,E, and F are missing from our list. We can now set the them as "N/A" in the output.
Here is the final code with output of the CSV.