Ezine 155 – PowerShell’s two or three meanings of Cmdlet
PowerShell is an efficient scripting language where each word is loaded with meaning. Cmdlet is the only word in PowerShell which causes me any ambiguity. In practice there is no problem because the context gives a clue as to the particular meaning of cmdlet. A built-in cmdlet is one verb-Noun command. Cmdlets that we create and store can be dozens of lines if you wish. In turn, third-party cmdlets specialise in specific task, for example QAD’s active directory cmdlets.
Cmdlet Topics
- Taking Stock
- This Week’s Mission
- Example 1 – Cmdlet meaning a built-in command
- Example 2 – Create your own Cmdlets
- Example 3 – Third party Cmdlets
- More Examples of PowerShell Cmdlets
Taking Stock
The dawning of a New Year is a classic time to take stock. In the case of my ezines, a reminder that every fortnight I send out two ezines; this ezine deals with specialist scripting topics, whereas the midweek ezine provides general computing tips and also reader’s recommendations for handy utilities.
I realize that over time readers needs change, if you don’t want to receive any more of these newsletters then either unsubscribe, or if you don’t trust the link at the bottom, reply to this letter and I will unsubscribe you from my end.
This Week’s Mission
My mission this week is to get back into PowerShell. Alternatively, if you are new to PowerShell, to get you started.
My main objective in this ezine is to explain the meaning of the PowerShell term ‘Cmdlet’. The first meaning is a built-in verb-noun pair such as get-Childitem, or out-File. The second meaning of Cmdlet is a textfile, with .ps1 extension, that stores up multiple lines of PowerShell code. What I like about these .ps1 scripts is that once you have perfected the commands, you can save the code and re-use it later, long after you forgot the original constructions. There is another meaning of Cmdlet, a third-party script which employs PowerShell to automate tasks such as creating users in Active Directory.
Example 1 – Cmdlet meaning a built-in command
PowerShell’s built-in cmdlets are the building blocks of all scripts. Here is a simple example to get you into the rhythm of the verb dash noun construction.
# PowerShell script to display the version type, or copy and paste:
get-Host
Note 1: As of January 2008, my version of PowerShell is 1.0.0.0. However, expect Microsoft to release new versions during 2008.
Note 2: Good old Microsoft, there are still three ways of doing everything, instead of get-Host, try plain ‘Host’ (get is assumed). Alternatively, type the name of the special variable: $Host.
Note 3: The situation with XP, Windows Server 2003 and Vista is that you have to download an operating system specific version of PowerShell together with .Net Framework. Then you have the simple task of double clicking on the downloaded files and installing them manually. Starting with Server 2008, PowerShell will be shipped with the operating system, but then you will have to use Add or Remove Programs to install it.
Note 4: To discover more built-in cmdlets issue this instruction at the PowerShell command line: get-command
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Example 2 – Create your own Cmdlets
In a word, the second meaning of cmdlet is script. The idea is to store a series of PowerShell commands in a plain text file. One benefit is that we can re-use the code without having to keep checking the syntax and testing the constructions. My choice of win32_share is not significant, I simply wanted a real-world example to show you how to construct your own cmdlets.
Before we tackle the task of listing a computer’s shares, remember that your cmdlets are unlikely to run on a brand new installation. This is because for security, by default, no PowerShell cmdlet (script) has permission to run. To control the situation you need to adjust the execution policy. Rather than hacking the registry, the best way to adjust the execution policy is to employ PowerShell itself.
2a – Adjust the Cmdlet (script) Execution Policy
# PowerShell script to control script behaviour:
set-ExecutionPolicy RemoteSigned
Note 1: Other values for set-ExecutionPolicy are ‘AllSigned’ or ‘Unrestricted’. To investigate try:
set-ExecutionPolicy -? or get-help set-execution -full
See more about the PowerShell set-ExecutionPolicy command
2b – Creating your actual Cmdlet (script)
Executing the cmdlet is either incredibly easy if you are a minor expert, or impenetrably difficult if you are a newbie. Once again, security is to blame. PowerShell is designed to prevent cmdlets running ‘accidentally’, or worse through hackers and phishers tricking unsuspecting users into executing their rogue PowerShell scripts. You cannot double click a PowerShell cmdlet and expect it to execute, instead you need a specific sequence namely: dot backslash filename. For example: .\share. Thus, if a hacker said ‘Type, dot slash then a filename’, even the most naive user may become suspicious, where they could easily be lured to click on a link to a rogue program.
I realize that others don’t work, or don’t teach like me, this is because I am obsessed with the twin goals of choosing real life tasks and building the final script slowly command by command.
Instructions
- Copy and paste the example code below into a text file
- Save with .ps1 extension, for example: share.ps1
- Launch PowerShell (if you haven’t already)
- Navigate in PowerShell to the directory where you saved share.ps1
- Type: .\share
# PowerShell script to list shares:
get-WmiObject win32_share
Note 1: My idea behind making such a ridiculously short cmdlet is that you could focus on the technique to execute the file. In particular pay attention to the folder where you save the text file with .ps1 extension. Also, because the code is simple you can concentrate on the rhythm of the execute command ./filename. In this instance ./share. Once you master the technique, then it opens up a whole new world of creating your own bigger, better and longer cmdlets.
2c – Creating a three line cmdlet
Instructions
As with example 2b, save the three lines below into a text file with a .ps1 extension, for example share3.ps1. Now call the file from within PowerShell with ./share3
$Share = get-WmiObject win32_share | where-object {$_.name -notlike "*$"}
$ShareSeq = $Share | Sort-Object -property path
$ShareSeq | ft path, name -autosize
Note 1: There are numerous ways of enumerating shares with PowerShell, the reason that I rejected the shortest most efficient method was to produce a script with multiple lines, thus making it worth saving into a cmdlet.
Note 2: One pleasant side effect of this long-winded cmdlet is that you can experiment easily. For example change:
where-object {$_.name -notlike "*$"} to: where {$_.name -like "*$"}
or change: Sort-Object -property path to: sort -property name
Guy Recommends: Tools4ever’s UMRA
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
Example 3 – Third party Cmdlets
A company called Quest has produced QAD cmdlets for PowerShell and made them available via the powergui.org website. Specifically, these cmdlets are designed to automate Active Directory tasks. The name or acronym QAD is both interesting and instructive. Q stands for Quest and AD means Active Directory.
In this ezine I merely want to introduce you the following points regarding third party cmdlets:
The fact that 3rd parties such as Quest create cmdlets, means that PowerShell is coming of age and has a future.
The fact that these third party cmdlets are for Active Directory, means that this is an area of weakness for PowerShell. To be fair, scripting Active Directory tasks with VBScript was even more difficult.
PowerGui Follow-up
I decided to practice what I preach, and use my PowerGui after a 2 month layoff. Any program that automatically installs updates impresses me.
Summary of PowerShell’s Cmdlets
Enjoy PowerShell’s cmdlets. Never miss an opportunity to learn another built-in cmdlet. Take the trouble to create a bank of your own cmdlets. Investigate the third-party cmdlets supplied free by Quest and PowerGui.
See more about PowerShell cmdlets
If you like this page then please share it with your friends
See More Windows PowerShell Examples of Real-life Tasks
• PowerShell Tutorials • PowerShell Examples • IpConfig • Get-Counter • PowerShell NetSh
• Monitor Performance – PowerShell • PowerShell temp • PowerShell Delete Temporary files
• PowerShell WOL (Wake-on-Lan) • Services • Change Computer Description Registry
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.