Displaying Logical Drives with PowerShell
PowerShell is a powerful scripting language and can do many things. One task that can be used over and over is to get the logical drives off a system. One you have this information you can then perform tasks like searching for files, directories, reporting usage etc..
In this post I will be simply showing how to create a variable and retrieve the data for the logical drives using WMI object win32_logicaldisk.
If we just enter Get-WmiObject win32_logicaldisk we see the output showing Device ID, DriveType, ProviderName, FreeSpace, Size and VolumeName.
PS C:\Windows\system32> Get-WmiObject win32_logicaldisk DeviceID : B: DriveType : 3 ProviderName : FreeSpace : Size : VolumeName : DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 59039604736 Size : 159724335104 VolumeName : Windows 7 DeviceID : D: DriveType : 5 ProviderName : FreeSpace : Size : VolumeName : DeviceID : E: DriveType : 5 ProviderName : FreeSpace : Size : VolumeName :
Now let’s take this a bit further and tell it we want DriveType 3. Which in this instance we see drive D: removed from the list, which is DriveType 5 (CD-ROM)
PS C:\Windows\system32> Get-WmiObject win32_logicaldisk| ?{$_.drivetype -eq 3} DeviceID : B: DriveType : 3 ProviderName : FreeSpace : Size : VolumeName : DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 59039604736 Size : 159724335104 VolumeName : Windows 7
So what can we do next? Well, how about let’s create a list of just the drive name? No problem, let’s build on the command and add the foreach-object and grab the name.
PS C:\Windows\system32> Get-WmiObject win32_logicaldisk| ?{$_.drivetype -eq 3} | foreach-object {$_.name} B: C:
Now we have something we can work with and can assign this to a variable that we can use in a script. Let’s take the line and add $drives = to the front of the command and create the $drives variable which we can reference.
$drives = Get-WmiObject win32_logicaldisk| ?{$_.drivetype -eq 3} | foreach-object {$_.name}
Now lets go a step further and get the first level of the directory for the drive. We need to do a foreach and use the Get-ChildItem cmdlet.
$drives = Get-WmiObject win32_logicaldisk| ?{$_.drivetype -eq 3} | foreach-object {$_.name} foreach ($drivename in $drives) { cd -Path $drivename Get-ChildItem $drivename }
So as you can see there are many other possibilities from what was touched on here. Have fun and keep PowerShelling !!!
8 Responses to “Displaying Logical Drives with PowerShell”
Trackbacks/Pingbacks
- Djoin, PowerShell, and Standalone Media | billamoore.com - […] shoutout to newlife007 over at LifeOfAGeekAdmin for a great little read on using PowerShell for displaying logical drives that inspired me to […]
This post was helpful to me today. For as simple as this is (?{$_.drivetype -eq 3} | foreach-object {$_.name}) it was really causing me fits. I will using something similar on a post I am working on for my blog about offline domain joining pc’s and will be sure to credit you appropriately. Thanks for posting this. You are now a part of my feed list. 🙂
bill
“I will *be”
To avoid feeding the trolls.
Thanks Bill I appreciate the feedback, I post scripts and How To’s from what I am using daily at my job as a System Admin. I plan to have more PowerShell and tricks in the future.
Hi,
Does this query also lists the NFS shares exposed from this windows machine?
The query is only for logical disks, if you want to see shares use Get-WmiObject Win32_share
Thanks!
But, Win32_Share is not returning me the NFS shares unless i map those to a network drive.
But, showmount -e is showing the nfs shares.
My doubt is– is it mandatory to map the nfs shared resource to a network drive?
Sorry I missed that in your previous question on the NFS Share. The Win32_Share is correct in what it is showing you for drives. It doesn’t know the NFS mount point but when you map it to a drive it will see it. There are NFS Cmdlets for viewing and working with NFS that is available for Windows 8.1 and Server 2012 R2 and higher found here https://technet.microsoft.com/en-us/%5Clibrary/jj603081(v=wps.630).aspx
I am using the below function to get the drives disk information from the remote servers. I am unable to get the few disk information. Could you please help me to get it.
$disks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer -Filter “DriveType = 3”