Windows PowerShell Drives and Namespaces
The  following articles provide a brief introduction to working with Windows  PowerShell drives and namespaces; these tasks include such things as  adding and removing Windows PowerShell drives and navigating through  Windows PowerShell namespaces. As is so often the case with Windows  PowerShell, the code snippets found in these articles can either be  included within a Windows PowerShell script or typed directly into the  Windows PowerShell console.
Using the Get-Location Cmdlet 
Checking Your Location in a Windows PowerShell Namespace
The Get-Location cmdlet simply lets you know where you are within the  various Windows PowerShell drives. For example, this command:
  Returns information similar to this:
 Path
----
C:\Documents and Settings\kenmyer
 If you’d like to know the current location in a different drive, just use the 
-psdrive  parameter following the call to Get-Location. For example, this command  leaves you in your current location, but tells you where you would be  if you used 
Set-Location to switch to the HKCU drive:
 Get-Location -psdrive hkcu
Using the New-PSDrive Cmdlet 
Creating a New Windows PowerShell Drive
The 
New-PSDrive cmdlet functions similar to the 
Subst  command: it enables you to map a drive (in this case, of course, a  Windows PowerShell drive letter) to a path. For example, this command  creates a new Windows PowerShell drive (drive X) that’s mapped to the  folder C:\Scripts:
 New-PSDrive -name X -psprovider FileSystem -root c:\scripts
 As you can see, New-PSDrive takes three parameters:
-   -name, which is the name to be given the new drive. We called our drive X  primarily because that’s easy to type, but you can give the drive any  name you want (just stay away from blank spaces and any restricted  characters).
 
-   -psprovider, which simply indicates the type of drive  being created. Because our new drive is part of the file system, we use  the FileSystem parameter. For other types of drives (such as registry  drives) you’ll need to use other providers. Fortunately, you can get a  list of drive providers by calling the Get-PSDrive cmdlet.
 
-   -root, which is, in this case, the folder that serves as the drive root. When we use the command Set-Location x, we’ll end up in C:\Scripts, the root of our new drive.
 
The upshot? Now you can switch to the C:\Scripts folder merely by typing this command:
  If that wasn’t enough, keep in mind that you aren’t limited to  creating new drives mapped to the file system; instead, you can map  drives within any Windows PowerShell namespace. For example, this  command maps Windows PowerShell drive Y to the  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion key in the  registry:
 New-PSDrive -name Y -psprovider Registry -root HKCU:\Software\Microsoft\Windows\CurrentVersion
 To access that registry key from now on you merely need to type 
Set-Location y:
Mapped drives last only as long as your current Windows PowerShell  session. If you exit PowerShell and then restart it, your mapped drive  will no longer be available. To ensure that your new drive survives  between Windows PowerShell sessions, create a PSConfiguration folder in  your Windows PowerShell profile folder. For example:
 C:\Documents and Settings\gstemp\My Documents\PSConfiguration
| Note. How are you supposed to know your Windows PowerShell profile folder? Just type this command: 
 Get-Variable profile | Format-List | 
In the PSConfiguration folder, create a file named Microsoft.PowerShell_profile.ps1 and add the following command to the file:
 New-PSDrive -name X -psprovider FileSystem -root c:\scripts
 The next time you start a Windows PowerShell session drive X will be  available to you. (Think of the profile file as being a sort of  autoexec.bat file for Windows PowerShell.)
Using the Get-PSDrive Cmdlet 
Listing Your Windows PowerShell Drives
Windows PowerShell maps a number of “drives” that make it easy for  you to navigate various namespaces on your computer; by default this  includes not only the physical disk drives on your computer but also  such things as the registry hives HKEY_CURRENT_USER (drive HKCU) and  HKEY_LOCAL_MACHINE (drive HKLM). To return a list of all the Windows  PowerShell drives on your machine, simply use the 
Get-PSDrive cmdlet:
  In return, Windows PowerShell will return information similar to this:
 Name       Provider      Root                                   CurrentLocation
----       --------      ----                                   ---------------
Alias      Alias
C          FileSystem    C:\                                 ...Settings\kenmyer
cert       Certificate   \
D          FileSystem    D:\
E          FileSystem    E:\
Env        Environment
Function   Function
HKCU       Registry      HKEY_CURRENT_USER
HKLM       Registry      HKEY_LOCAL_MACHINE
Variable   Variable
 Alternatively, you might want to pipe these results into the Format-List  cmdlet; when you do that, you’ll get back additional information (such  as the full provider name) for each drive. That command looks like this:
 Get-PSDrive | Format-List
 And the returned data will look like this:
 Name            : Alias
Description     : Drive containing a view of the aliases stored in session state.
Provider        : Microsoft.Windows PowerShell.Core\Alias
Root            :
CurrentLocation :
Name            : C
Description     :
Provider        : Microsoft.Windows PowerShell.Core\FileSystem
Root            : C:\
CurrentLocation : Documents and Settings\kenmyer
 You can also specify an individual drive name (or use wildcard  characters) to return information about a single drive or set of drives.  This command returns information for only drive C:
  And this command returns information for the two registry drives (both of which have names starting with the letter H):
   
Using the Set-Location Cmdlet 
Navigating through Windows PowerShell Namespaces
The 
Set-Location cmdlet is roughly equivalent to the  cd command found in Cmd.exe: it enables you to specify a new working  location in a namespace. For example, this command sets the working  location to C:\Scripts:
  So why did we use clumsy terminology like working location rather  than simply saying that Set-Location enables you to set the current  working folder? That’s because, in Windows PowerShell, you aren’t  limited to navigating through the file system (for more information, see  the Get-PSDrive cmdlet). For example, this command places you in the HKEY_CURRENT_USER\Software\Microsoft\Windows portion of the registry:
 Set-Location HKCU:\Software\Microsoft\Windows
 Incidentally, not only can you use the alias 
cd as a shortcut method for calling Set-Location, but you can also use the commands 
cd .. to move to the parent location and 
cd \ to move to the drive root.
Using the Convert-Path Cmdlet 
Translating a Windows PowerShell Path
The 
Convert-Path cmdlet converts a Windows  PowerShell path to a system path. What does that mean, and does it even  matter? Well, suppose you’ve created a new Windows PowerShell drive  (drive X). That drive letter is valid only in Windows PowerShell; you  can’t switch to Windows Explorer and access drive X. Convert-Path,  however, can tell you the “real” path of drive X. In other words, if you  run this command:
  You’ll get back information similar to this:
  This is particularly useful with registry drives. Windows PowerShell  has its own syntax for indicating registry paths; for example, to use  the Set-Location cmdlet to switch to the registry you would use a command similar to this:
 Set-Location hkcu:\software\microsoft\windows
 That’s fine, but outside of Windows PowerShell a path like 
hkcu:\software\microsoft\windows  is meaningless. If you need to reference that path outside of Windows  PowerShell, then just ask Convert-Path to lend you a hand:
 Convert-Path hkcu:\software\microsoft\windows
 That command gives you the actual path within the registry:
 HKEY_CURRENT_USER\software\microsoft\windows
Using the Remove-PSDrive Cmdlet 
Unmapping a Windows PowerShell Drive
Nothing lasts forever, which is as true for Windows PowerShell drives  as it is for anything else. If you ever decide to get rid of a Windows  PowerShell drive then simply call the 
Remove-PSDrive cmdlet, passing along the name of the drive to be removed. For example, this command removes drive Y:
   
 
No comments:
Post a Comment