Exploring Get-PSDrive in PowerShell
Welcome back to Wahmans Powershell blog! Today we’ll take a closer look at a very helpful PowerShell cmdlet: Get-PSDrive
. Whether you are just starting out with PowerShell or are already writing advanced scripts, understanding this cmdlet can help you better manage your resources and file systems.
What is Get-PSDrive?
According to Microsoft’s documentation, Get-PSDrive
gets drives in the current session. But this isn’t just limited to your file system drives like C:
or D:
— PowerShell drives can also expose data from other providers such as the registry, environment variables, and more.
Let’s explore some usage examples
1. Basic Usage: List All Drives
When you simply run Get-PSDrive
, it lists all available drives currently in the session.
Get-PSDrive
This will show you drives like C:
, D:
(for file system), Env:
(environment variables), HKLM:
(registry), etc.
2. Filter Specific Drive Type
You might want to list only file system drives. You can filter using the Provider
parameter:
Get-PSDrive -PSProvider FileSystem
This command returns all file system drives like C:
, D:
, or mapped network drives, excluding drives like Env:
or HKCU:
.
3. Check Free and Used Space
Each file system drive includes information such as used and free space. You can use this to check available space on a drive:
$drive = Get-PSDrive -Name C
"Drive C has $($drive.Free / 1GB) GB free space and $($drive.Used / 1GB) GB used space."
This outputs the amount of free and used space on the C: drive, converting bytes to gigabytes.
4. Create a Custom PSDrive (Advanced)
You can also create your own custom drive using New-PSDrive
, for instance to map a drive to a registry path or network share, and use Get-PSDrive
to verify it:
New-PSDrive -Name MyEnv -PSProvider Environment -Root ""
Get-PSDrive -Name MyEnv
This creates a new drive called MyEnv:
that maps to environment variables. You can now browse it like a normal drive using cd MyEnv:
.
Wrap-up
Get-PSDrive
is a powerful and flexible cmdlet that provides insight into both traditional and non-traditional drives in PowerShell. Whether checking disk space or exploring environment variables, this tool is a must-know for every PowerShell user.
Happy scripting, and I will see you in the next post!
Leave a Reply