Powershell offers several ways to save yourself some typing and shorten your command lines and scripts. Plus you almost never blow yourself up.
One way is to string together all of the slicing and dicing you might need to do. If a method for an object returns an array and I know I need a specific element in that array I can specify that element right after the method invocation. And I can call a method right from that array element. Allow me to demonstrate.
In a previous example I had to parse the DistinguishedName for a user. I model my test domain based on a former employer who managed users and computers by location. Delegations were done so support staff at each site could manage their objects. My OU structure looks like this:
In order to find the site name for an arbitrary user account I can assign a variable at each step to parse the DistinguishedName. In this example I also show the value of the variables to confirm each step:
But I don't need to use all those variables. This can be processed in a single line. Below I build up each step. The last line shows how I combine all of these steps into a single command:
Another handy shortcut is to enclose a command in parentheses and do the same slicing and dicing. In the above example I don't even need to assign the Get-ADUser output to a variable. I can use the command
(Get-ADUser AlAlpha).DistinguishedName.split(',')[2].substring(3)
Letting PowerShell process things in parentheses has many uses and works for all cmdlets. You can nest some really crazy crap and it will still work. In the following example I know I have a user in Atlanta with the last name Alpha and I'm not sure what the account name is but I have to change his first name to Allen. The first command get-aduser -searchbase "OU=Atlanta,OU=Locations,DC=toddp,DC=local" -filter * | where {$_.surname -eq 'Alpha'} shows how I can do this search. In the second command I enclose that first command in parentheses and use that as the identity for my Set-ADUser command. I call Get-ADUser one last time to show the change was made in the GivenName attribute.
Another PowerShell shortcut is to use aliases for the cmdlets.
Here we see all of the aliases that start with the letter "g". Two aliases I use frequently are gm for Get-Member and gwmi for Get-WmiObject.
In this example the Get-ADUser output is piped through the Get-Member cmdlet using the alias GM.
You can create your own aliases using the Set-Alias cmdlet.
After using Import-Module (or its alias, ipmo) to import the ActiveDirectory cmdlets I use Set-Alias to create the alias gadu for the Get-ADUser command. Note that an alias can only be for a cmdlet, not a cmdlet and its parameters. You can work around this limitation by using a function as shown in the last example for Set-Alias.
There are two other aliases that you may encounter.
There is a default alias for ForEach-Object which is the percent sign (%) and an alias for Where-Object which is the question mark (?). This will come in handy.
How cool is this? I pipe Get-Process through the ForEach-Object shortcut to pull out the ProcessName and use the Where-Object shortcut to show just the processes starting with the letter "c". That's good enough for me.
In Powershell 3 there is an implied ForEach-Object for arrays. Instead of piping the array through ForEach-Object cmdlet or shortcut you just enclose the object in parentheses and process the properties you need.
These are the processes running on my Windows 8 demo VM. But if I need to grab just the process name I can use the syntax (Get-Process).ProcessName
Pretty sweet. There are some other handy shortcuts coming in PowerShell 3.
So we have all these nice shortcuts. Shouldn't we use them all the time? The rule of thumb from our friend Don Jones is that if you are writing a quick and dirty command for yourself, then the shortcuts can save you time and effort. But scripts with shortcuts look more cryptic and are harder to read. So if you are creating a more invovled script that will be used by someone else you should be considerate and make it more accessible by avoiding shortcuts.
Knowing these shortcuts and aliases (or at least how to find what is being aliased) helps you be more efficient. And if you come across a script written by someone else who used them you won't be caught off guard.
No comments:
Post a Comment