Subscribe to Windows IT Pro
June 26, 2008 12:00 AM

PowerShell 101, Lesson 6

How to work with PowerShell's built-in drives and create new drives
Windows IT Pro
InstantDoc ID #99104
Rating: (1)

New-Item `
HKLM:\Software\Microsoft\TestKey1

This command creates the TestKey1 key in HKEY_LOCAL_MACHINE\Software\Micro soft. Figure 7 shows this command’s results.

After you create the key, you can use the New-ItemProperty cmdlet to add a property to the key. (Adding a property in PowerShell is the same as adding an entry in the registry editor.) The following statement adds the TestProperty property to TestKey1:

New-ItemProperty `
HKLM:\Software\Microsoft\TestKey1 `
-Name TestProperty -PropertyType string `
-Value “test value”

The added property has a value of test value, which is a string data type. When you run the statement, PowerShell returns a list of all properties and their values. As Figure 7 shows, the new property has been added.

You also can take other actions through the PowerShell drives. For example, the following command uses the Rename-Item cmdlet to rename TestKey1 to TestKey2:

Rename-Item `
HKLM:\Software\Microsoft\TestKey1 `
TestKey2

In this command, the first argument identifies the original key and the second argument provides the new name. You can also use the Remove-Item cmdlet to remove a registry key:

Remove-Item `
HKLM:\Software\Microsoft\TestKey2

As these statements demonstrate, working with a registry drive is similar to working with a file-system drive. You can just as easily use the New-Item, Rename-Item, and Remove Item cmdlets with files and folders—or items in any other drive for that matter.

Creating PowerShell Drives
Up to this point, I’ve shown you only statements that use the built-in PowerShell drives. However, you can also create drives based on existing providers. This can be useful when you want to simplify commands that you use often.

To create a PowerShell drive, you use the New-PSDrive cmdlet. For example, the following statement creates a drive named ps:

New-PSDrive -Name ps `
-PSProvider FileSystem -Root $pshome

The statement identifies the name of the new drive, then the provider, and finally the root. In this case, I use the PSHOME built-in variable to retrieve the PowerShell home folder name. When you run this statement, PowerShell creates the drive and displays information about the drive, as shown in Figure 8. Notice that PowerShell displays the actual root name, not the variable name. (For more information about variables, see Lesson 5.)

After you’ve created your drive, you can use it just like the built-in drives. For example, the following statement changes the working location to the ps drive:

cd ps:\

As Figure 8 shows, the PowerShell command prompt reflects the name of the new drive. You can now work in this drive as though you had changed the working location to C:\Windows\system32\Windows PowerShell\v1.0.

To test whether you’re working in the correct folder, you can run the Get-ChildItem cmdlet. Figure 9 shows you the type of results you should expect. Notice that the results include the correct name of the working location. (In this example, PowerShell is running on a Windows XP computer. If you run PowerShell on a different OS, you might see different results because the Power- Shell home directory is set up differently for different OSs.)

PowerShell also includes the Remove- PSDrive cmdlet, which lets you remove user-defined drives. To use the cmdlet, you must be in a working location other than the one you want to delete. For example, the following code changes the working location, then deletes the ps drive:

cd C:\; Remove-PSDrive ps

Note that any drives you create within a session persist only until you end that session, so you don’t need to remove a drive unless you have a reason to explicitly delete it. For example, you might want to simplify your list of available drives when you’re no longer using a particular drive. You can persist custom drives across sessions by modifying your profile file. In a later lesson, you’ll learn how to create and customize profile files. However, if you’re anxious to learn more about profile files now, see the TechNet article “Window PowerShell Profiles” (technet.microsoft.com/en-us/library/cc162758.aspx).

That’s All for Now
In this lesson, I introduced you to Power- Shell providers and drives. As the examples demonstrate, you can access a number of data stores in a manner similar to accessing files and folders. In addition, because PowerShell uses providers and drives, the methods used to access data are consistent among the data stores. In fact, much of what you’ve learned in the PowerShell 101 series can be applied to the various drives. When the PowerShell 201 series begins, you’ll be able to use the information from this lesson as well as the other lessons to create complex statements that can access and manipulate a wide range of resources.

Related Content:

ARTICLE TOOLS

Comments
  • Martin
    4 years ago
    Jul 21, 2008

    Hi there.
    Great article. All examples worked, at least second time around. I've been following these articles and I enjoy them.
    But give the referencesto web-cast on the topic. They are great fun, instrucional and not at least inspirational and motivating.
    As for instance Sripting for dummies http://www.microsoft.com/norge/technet/spotlight/sessionh.aspx?videoid=214&PUID=000115874C3F7258 and
    Windows, PowerShell, and Windows Management Instrumentation: Unveiling Microsoft's Best Kept Secret - Ben Pearce - 07/08/2008
    http://www.microsoft.com/norge/technet/spotlight/sessionh.aspx?videoid=996

    Try them. They are great.

    Regards,

    Martin T
    Norway

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.