|
||||||
An Introduction to the Windows PowerShellA Quick Tutorial on Work with Microsoft's PowerShellPowerShell is Microsoft's new shell language. It works with the .NET objects and is therefore much more powerful than MS-DOS or CMD, but much simpler than VBScript.
With every new version of Windows there appears to be a new interpretation of the Windows shell. First there was MS-DOS, for a while it’s been CMD, and now it’s PowerShell. However, Microsoft seems to have made a major breakthrough with PowerShell. It is not just another scripting language instead it works with .NET objects. And it does this by using Cmdlets. What is a Windows PowerShell Cmdlet?A Windows PowerShell Cmdlet (or Command-let) is the means by which .NET objects can be accessed from the command line. A Cmdlet can easily be recognized from the fact that its name will consist of two elements: a verb and a noun. For example, one of the most useful Cmdlets is: Get-help
However, there are abbreviations for some of the Cmdlets, for example: Get-Process
Can also be typed as: ps
Which is command familiar to many Linux users as the command to view details about currentlyrunning processes. Interestingly there are a number of other Linux-PowerShell parallels, for instance:
Of course, there is a small issue here (as some readers will have already discovered): PowerShell is not installed by default. Obtaining PowerShellPowerShell is installed in Windows Server 2008 and is also part of Windows 7, but it does not come ready loaded with XP or Vista. Users of these platforms will need to download the PowerShell installation program from Microsoft’s How to Download Windows PowerShell 1.0 web page. Running PowerShellOnce PowerShell has been installed then it can be run in two ways:
At which point the user can start using the PowerShell commands. A Simple PowerShell ExampleOne very useful Cmdlet is ps (or get-process). This lists the currently running processes and details such as the number of pages of memory used by each process and the percentage of processor usage. The result will be something like: Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
86 3 1128 416 32 1.09 1016 alg
245 9 4948 980 51 7.96 3412 AutoUpdateSrv
45 3 2092 0 31 0.31 3772 cmd
505 6 1972 788 30 143.55 452 csrss
295 8 3592 1168 50 1.97 3436 dllhost
The list is sorted alphabetically according to the process name. However, it may be more useful to sort by the WS field so that the processes using the most memory appear at the top of the list. To do this the user “pipes” the output of ps to a second Cmdlet “sort-object”: ps | sort-object WS –descending
Now the result will be something like: Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
463 9 57420 10968 182 90.98 2176 powershell
285 9 6880 9072 126 232.47 2792 WINWORD
1837 55 17388 7524 113 230.13 840 svchost
515 14 19740 6332 87 170.92 1404 explorer
696 19 17360 5116 109 54.59 3388 IEXPLORE
567 64 7624 2364 55 8.17 476 winlogon
If the output is examined then “powershell” can be seen as one of the processes. Obviously the user will not want to see that, and so a filter can be added. It’s also worth noting that the piped Cmdlets can be split over multiple lines: ps |
where-object -FilterScript {$_.processname -ne "powershell"} |
sort-object WS –descending
Finally the concatenation symbol (>) will send the result output file after the information has been converted to HTML so that it can then be viewed in a web browser: ps |
where-object -FilterScript {$_.processname -ne "powershell"} |
sort-object WS –descending |
convertto-html -property Name,WS > ps.html
The result will be an HTML file containing only the alphabetically sorted, currently running processes and the amount of memory that they’re using.
The copyright of the article An Introduction to the Windows PowerShell in Command Line Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to the Windows PowerShell in print or online must be granted by the author in writing.
Comments
Sep 11, 2009 9:11 AM
Guest :
1 Comment:
|
||||||
|
|
||||||
|
|
||||||