Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


July 2008

PowerShell 101, Lesson 6

How to work with PowerShell's built-in drives and create new drives
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
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.

End of Article

   Previous  1  2  [3]  Next  


Reader Comments
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

toremf July 21, 2008 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Learning Path To read the previous PowerShell 101 lessons, go to
"PowerShell 101, Lesson 1"

"PowerShell 101, Lesson 2"

"PowerShell 101, Lesson 3"

"PowerShell 101, Lesson 4"

"PowerShell 101, Lesson 5"


Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Related Articles PowerShell Empowerment

Essential Windows PowerShell Commands

PowerShell Scripting

Dig Out by Digging Into PowerShell

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement