[PowerShell Getting Started] provider and drive _powershell

Source: Internet
Author: User
Providers and drives

PowerShell series has not updated for a long time, the main reason is that some of the topics involved in lazy is too much, for a while do not know how to organize into the article sent out. Recently just because of a little problem and toss a bit of PowerShell, by the way to organize such an article, introduced PowerShell in the very important provider and driver. Provider (Provider)

The word provider is translated as a "provider" in many Chinese materials, and I was like this when I first learned the word PowerShell:

To understand this concept, it is only possible to read its name in English. Provider, provider, what to offer. In PowerShell, provider provides data. The data it provides can be a file, an environment variable, or registry information. Speaking of which, you may still be a little confused. But it doesn't matter, you just have to remember that it is providing the data on the line, please continue reading. Drive (Drive)

What about the drive? When it comes to drives, most people would think of a variety of "cdef disks" first, but PowerShell drives are not that simple. If the provider provides data, the drive is the way it provides indexed data, or the drive is connected to the media that stores the data through a specific provider.

For example: FileSystem is a PowerShell built-in provider that provides file system-related data, while the C disk is a drive that belongs to filesystem, through C:\path\to\ In the form of file we can index to the data (file or folder) provided by the provider.

The drive in PowerShell is a many-to-many relationship to the provider, just as a provider that provides file data can correspond to multiple file system drives (Cdef disks). Item (item)

Take a closer look at the composition of data sources such as file systems, registries, environment variables, we can see that there are many similarities between these systems: they are all tree structures. Each data corresponds to a path, such as a file path and a registry key path (this feature is based on its tree structure), and each data is by name, Content and attributes.

So PowerShell has introduced another concept--items (item). PowerShell abstracts the data provided by the provider into a single item (the file is an item, the folder is an item, the registry key is an item, and the environment variable is an item), and the data is unified as an item.

PowerShell in this form, the data is exposed in a consistent manner so that we can manage the different types of data in the same way. It is interesting to think that you can modify the environment variables in the same way that you modify the contents of the file. Built-in providers and drives

PowerShell has the following providers and drives built in: Provider Drive Data Store alias Alias:cmdlet alias Certificate Cert: Digital Signature Certificate Environment ENV: Environment variable F Ilesystem * File and folder function Function:powershell functions Registry HKLM:, HKCU: Registry, where two drives HKLM: corresponding registry HKEY_LOCAL_MACHINE,HKCU : Corresponds to variable wsman wsman:ws-management configuration information in HKEY_CURRENT_USER Variable Variable:powershell

With these built-in drives, you can access and manage the data provided by your provider:

PS > Set-content env:\test Test
PS > get-content env:\test
Test
PS > Get-content variable:\pid
27631

Note the drive name in the path is preceded by a colon (:), and the path separator on Windows uses a backslash (\). Related cmdlet get provider

Get-psprovider [[-psprovider] <string[]>] [<commonparameters>]

Use the Get-psprovider command to get the provider object (System.Management.Automation.ProviderInfo). Parameter-psprovider can specify the name of the provider to get, you can use wildcard characters, and get all provider objects without specifying.

PS > Get-psprovider e*, f*

Name                 capabilities                            drives
----                 ------------                            ------
Environment          shouldprocess                           {ENV}
filesystem           Filter, shouldprocess, Credentials      {C}
function             shouldprocess                           {function}

You can use Get-member to obtain properties and methods for the provider object:

PS > Get-psprovider |             Get-member TypeName:System.Management.Automation.ProviderInfo Name membertype Definition---- --------------------equals method bool Equals (System.Object obj) GetHashCode method int Gethash   Code () GetType method Type GetType () ToString Method string ToString () capabilities Property
System.Management.Automation.Provider.ProviderCapabilities Capabilit ...
Description Property string Description {Get;set;}
Drives Property System.collections.objectmodel.collection[system.management.automati ...
HelpFile Property string HelpFile {get;}
Home Property string Home {Get;set;}
Implementingtype Property type Implementingtype {get;}
Module Property Psmoduleinfo module {get;}
ModuleName Property string ModuleName {get;}
Name property string name {get;} PSSnapin Property System.Management.Automation.PSSnapIninfo pssnapin {get;} 
Capabilities: Features supported by the provider
Credentials indicates that the use of configurable credentials to connect to the data store Filter indicates that the operation of the item supports the-filter parameter shouldprocess represents the operation of the item support-WHATIF and-confirm parameters transactions Represents support transaction Description: Description Information drives: The provider contains the drive object HelpFile: The path to the Help file home: The path corresponding to the "~" number in the corresponding drive Implementingtype: the specifically implemented class PSSnapin: Provides components for this provider to obtain drive

get-psdrive [-literalname|[ -name] <string[]>] [-psprovider <string[]>] [-scope <string>] [-usetransaction] [< Commonparameters>]

Use the get-psdrive command to get the drive object (System.Management.Automation.PSDriveInfo). The parameter list is as follows:-literalname is used to specify the name of the drive, which specifies that the drive ignores the wildcard-name is also used to specify the drive name, but when using this parameter, you can use wildcards to list all the drives without specifying the drive name- Psprovider specifies which providers to get the drive from, and you can use the wildcard-scope to specify the scope of the drive, which has global, local (default), script, and relative current range digits (0 is the current range, 1 is the parent)- UseTransaction the command into a transaction, which only works when the transaction is in progress

Again, we use Get-member to see what information is saved in the drive object:

PS > Get-psdrive |            Get-member TypeName:System.Management.Automation.PSDriveInfo Name membertype Definition----
--------------------CompareTo method int CompareTo (System.Management.Automation.PSDriveInfo d ...)
Equals method bool Equals (System.Object obj), bool Equals (System.manag ...)         GetHashCode method int GetHashCode () GetType method Type GetType () ToString method
String ToString () credential Property pscredential credential {get;}
Currentlocation Property string Currentlocation {Get;set;}
Description Property string Description {Get;set;}
Displayroot Property string Displayroot {get;}
MaximumSize Property System.nullable[long] maximumsize {get;}
Name property string name {get;}
Provider Property System.Management.Automation.ProviderInfo Provider {get;} Root property string Root {GET}
Free Scriptproperty System.Object Free {get=## Ensure, this is a filesys ... Used Scriptproperty System.Object Used {get=## ensure.
Credential: Specify credentials, or you cannot view a drive created with another user currentlocation: Record the current location and jump to this location when you return to the drive from another drive Description: Provide descriptive information Displayroot: This parameter is empty in the built-in drive, so temporarily not clear function, if you know its function, please contact Bo main maximumsize: This parameter is empty in the built-in drive, so temporarily not clear function, if you know its function, please contact blogger Name: drive name Provider: Which provider the drive belongs to: Root path Free: Available space (filesystem only) Used: Space Used (only filesystem) new drive

new-psdrive [-name] <String> [-psprovider] <String> [-root] <String> [-confirm] [-credential < PSCREDENTIAL>] [-description <string>] [-persist] [-scope <string>] [-usetransaction] [-WhatIf] [< Commonparameters>]

Create a drive that is associated to a network or local location. The drive created by this command is divided into a temporary drive or a network mapped drive, which can be saved to the local host. -name Specify the name of the new drive, for the network mapped drive, you need to type a letter, others can specify the provider with any name-psprovider, the temporary drive can associate any provider, the network map drive must be filesystem- root specifies the root path of the new drive, the temporary drive can be any path, and the network mapped drive can only associate the remote host's file path (using a UNC path)-CONFIRM Displays the confirmation action when running the cmdlet- Credential specifies a user with permission to execute the command, by default to the current user, by entering a user name or a Pscredential object generated by get-credential, which can be used when the-root parameter is a UNC path- Description Specifies the description information for the drive-persist indicates that this command creates a network mapped drive and that the drive is not saved to the operating environment-scope the specified drive when the script is run without using the DOT syntax, and the optional values are global, Local (default), script, and the current range of digits (0 is the current range, 1 is the parent)-usetransaction puts the command into the transaction, which only works when the transaction is performed-whatif display debugging information and does not actually create the drive when you add this parameter

PS > New-psdrive-name me-psprovider filesystem-root C:\Users\ganzi

Name   Used (GB) free   (GB)   Provider     Root
----   ---------   ---------   --------     ----
me          0.00       62.58   filesystem   C:\Users\ganzi
Remove Drive

remove-psdrive-literalname| [-name] <String[]> [-confirm] [-force] [-psprovider <string[]>] [-scope <string>] [-usetransaction] [ -WHATIF] [<commonparameters>]

This command removes the temporary drive created by the new-psdrive command and disconnects the connection mapped to the network drive, which cannot remove the physical drive. -literalname is used to specify the name of the drive, using this parameter to specify that the drive ignores the wildcard-name is also used to specify the drive name, but use this parameter to use the wildcard character-confirm to display the confirmation action when running the cmdlet- Force Force Delete-psprovider Specify which providers to remove the drive from, you can use the wildcard character-scope specify the scope of the drive deletion-usetransaction put the command into the transaction, which only works when the transaction is in progress- WHATIF displays debugging information and does not actually delete the drive when you add this parameter

This command can also enter the Psdriveinfo object through the pipe:

PS > Remove-psdrive-name SMP
ps > Get-psdrive X, S | Remove-psdrive
Tips If you need the details of one of these providers, you only need to run the Get-help $ProviderName-full provider is provided by the snap-in (snap-in), although the provider cannot be deleted. However, you can use Remove-pssnapin to remove an effect provider that removes a provider can dynamically add parameters to the built-in cmdlet, which is available only when the cmdlet and provider data are used together. For example, when using the Get-item and Get-childitem commands in the Cert drive, you can use the-codesigningcert parameter when a new drive is plugged into the computer, PowerShell automatically adds a drive object to the filesystem provider without requiring a reboot, and PowerShell automatically removes the object when the drive is disconnected

About the registry provider has only two root nodes, there are no HKEY_CLASSES_ROOT and other nodes of the problem, pstips on the explanation:

In fact, the HKEY_CLASSES_ROOT root node is not a separate root node, but it points to hkey_local_machine\software\classes.

You can use a script to add a special folder to a virtual drive: PowerShell Add a personalized drive summary

PowerShell the data by providing a combination of program-drive-item combinations, allowing us to manage data in the same way, but with similarities, from different sources. It has to be said that this design is much more advanced than other products of the same kind, give full play to the advantages of PowerShell object-oriented, but it also makes learning cost much higher.

The next plan is to talk about the operation of PowerShell, and if it's not an accident, we'll see you in a week. Reference PowerShell Official Document "Windows PowerShell Combat Guide" Pstips

Thank you for the above content provider.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.