What's your top Powershell command? Let's make a list and vote up the best ones!
I like having a script for finding AD users:
$strFilter = "(&(objectCategory=User)(sAMAccountName=[USERNAME]))"; $objSearcher = New-Object System.DirectoryServices.DirectorySearcher; $objSearcher.SearchRoot = "LDAP://dc=[AD_Location]"; $objSearcher.Filter = $strFilter; $objSearcher.SearchScope = "Subtree"; $objSearcher.FindALL()
For me -WhatIf is the most useful.
I see Get-Member listed which I have to agree is the best but a near and equally neccessary second for me is:
The way I more commonly use this is after a pipe to show me all the values of the properties of an object:
$SomeObject | fl *
GM will tell you what the object is and list out the names and types of its properties but more often than not this does not help you really understand the way those properties are used without looking up the documentation which can be tedious.
$SomeObject | Format-List * will output all of the values of the properties available on the object so that you can see what their current values are and start looking for properties that you want to select or use to filter objects on.
I will often find that an object has multiple properties that based on the documentation look like they have the data I want but in reality only one of the properties has data or the data in one of them is not at all what you would expect based on the documentation alone.
I did just a small method that i added to my Profile. See http://blog.keystroke.ch/2007/08/30/diskusage-in-powershell/.
is my prefered command.
In addition to Get-Member, I would have to say Add-Member and New-Object which give you the ability to use the Extensible Type System.
Being able to add members to any object at any time allows you to get the exact information you need with the ability to sort it, group it, slice it, dice it, export it, or whatever you want to do with it.