Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 log on before posting a comment.

If you don't have a username & password, please 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
Command Prompt Tricks

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

How can I stop and start services from the command line?

...

New Microsoft/Yahoo! Deal? No

On Sunday, the Times of London reported that Microsoft had renewed talks with failing Internet giant Yahoo! and would manage its search engine for 10 years, while Yahoo! would retain control of its email, messaging, and content services. This report ...


Related Articles PowerShell Empowerment

Essential Windows PowerShell Commands

PowerShell Scripting

Dig Out by Digging Into PowerShell

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events SQL Server 2008 – Can You Wait? | Philadelphia

PowerShell 101 - eLearning Series

SQL Server 2008 – Can You Wait? | Atlanta

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing