Thursday, February 8, 2018

Windows PowerShell Basics - How To List PSDrive Info


Overview

A Windows PowerShell Drive (PSDrive) is a data store location that you can access like a file system drive in Windows PowerShell.

The Windows PowerShell providers create some drives for you, such as;.
  • File System Drives (including C: and D:),
  • Registry Drives (HKCU: and HKLM:),
  • Certificate Drive (Cert:).
You can create your own Windows PowerShell drives as well. These drives are very useful, though they are available only within Windows PowerShell.

You cannot access these drives by using Windows tools, such as File Explorer or Cmd.exe.

Applies To

Tested on Windows 10, Windows 2008 R2, Windows 2012.

Pre-Requisites

To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.

Policy Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.

Current Execution Policy

To know the current run the PowerShell cmdlet; Get-ExecutionPolicy

To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List



PowerShell Script – PSDrive Info

This PowerShell script demonstrates retrieving PowerShell Drive (PSDrive) Information in PowerShell Console.

Windows PowerShell uses the noun, PSDrive, for commands that work with Windows PowerShell drives, also you can retrieve information as below;

Get-PSDrive | Format-Table -AutoSize



PowerShell Snippet

The code snippet is for demonstrating “PSDrive” information listing, Windows PowerShell uses the noun, PSDrive. This script will retrieve the available PowerShell drives on to a HTML file.


Clear-Host
#
# Set Output Path and File
#
Set-Location -Path $env:USERPROFILE
Push-Location -Path $env:USERPROFILE
$OutputFile="PSDrives_Info.html"
#
# Remove Old File 
#
if ($OutputFile) {
Remove-Item $OutputFile

}
# 
# Set HTML Header
#
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH    {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: green;}
TD    {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header | Out-File -FilePath $OutputFile
Invoke-Item $OutputFile

PowerShell Output – PSDrive

When script is executed; below output will be displayed.



SlideShare Information

A step by step guide is loaded to SlideShare.



No comments:

Post a Comment