WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Top 10 Tips for Using Windows PowerShell
Pages: 1, 2, 3, 4, 5

9. Use flow controls and scripts

The PowerShell language is powerful and supports scripting.

Many commands take scripts as parameters. The Where-Object command is one example. I showed you this code earlier:

Get-Process | Where-Object {$_.VirtualMemorySize -gt 104857600}

The For-Each object can execute a script for each item in a list. Here's a simple example:

PS C:\WINDOWS\system32> 1..10 | foreach { $_ * 2 }
2
4
6
8
10
12
14
16
18
20

This isn't particularly useful by itself. But the technique is applicable in many places. For example, suppose you want to copy every text file in a directory that has the string #backup in the first line to the c:\backups directory. Here's how you could do it:

dir *.txt | foreach { if ((Get-Content $_ -totalCount 1) -eq "#backup") { copy $_ c:\backups } }

This line starts with dir (or Get-ChildItem) and pipes the results through the foreach command. The foreach command runs the script shown in curly braces for each file encountered. The script reads the first line of each file (via Get-Content, passing -totalCount 1 to get only the first line), and compares it to the string #backup. If the string matches, the script copies the file.

10. Customize your environment

You can customize your environment by saving functions and changes in the profile.ps1 files.

When you develop your own scripts to do the work you need, you'll probably want to save them for later. You might also want to create more aliases of the commands you find userful. For example, in the previous section I created a script that will back up text files that start with #backup.

You can put your customizations in your own profile. The previous script would need to be a function such as this:

function Backup-TextFiles {
    dir *.txt | foreach {
        if ((Get-Content $_ -totalCount 1) -eq "#backup") { 
            copy $_ c:\backups } 
        }
    }

This function would then go in your profile. The profile is a file called Profile.ps1, and it goes in your home directory, which is \Documents and Settings\<username>\My Documents\WindowsPowerShell\profile.ps1 where <username> is replaced by your actual username.

However, before you can run the script, it must be signed for security. (Alternatively, you can turn off the security restrictions. Several blogs are saying to do this, but that's a really bad idea.) This is a great link that explains how you can sign your script. (In the article, the author mentions adding a snap-in to MMC. To get there in mmc.exe, click File->Add/Remove Snap-in. Make sure when you run the makecert.exe utility, you're in the directory that the author mentions contains makecert.exe. Also, I had to refresh the MMC window before my changes appeared.)

Conclusion

The new Windows PowerShell is big, but don't be overwhelmed by it. When I first started using it, I couldn't imagine how it could make my life easier. There were too many unfamiliar commands and the commands seemed very long to type. But in a short time, I started to learn the commands and aliases, and understand the whole way of thinking in the PowerShell world. I'm glad Microsoft has finally given us this tool and excited to see what new tools the community will build around PowerShell to help make our lives easier. Enjoy!

Jeff Cogswell lives near Cincinnati and has been working as a software engineer for over 15 years.


Return to the Windows DevCenter.