What's your top Powershell command? Let's make a list and vote up the best ones!
closed as not constructive by Mark Henderson♦ Jan 15 '12 at 5:38
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.
locked by HopelessN00b Dec 5 '14 at 4:11
This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.
Read more about locked posts here.
Get-Member is the king of PowerShell cmdlets.
Get-Member allows you to interactively explore objects' members and types' (with the -static switch) static members.
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()
I have to go with get-help. This cmdlet is the key to finding the functionality of other cmdlets. A close second would be get-member.
At the moment I'm a big fan of Get-WMIObject.
"Test-Path" finding this useful for checking if data is already there or if a default needs to be added.
For me -WhatIf is the most useful.
CD (Change-Location) - I can basically navigate around registry as if it is a file system. basically you can do so for all powershell providers (returned by Get-PSProviders)
No question about it - Get-Member. I can't imagine any PowerShell session without using it. Discoverability is one of the strongest features of Windows PowerShell.
This is more of a technique than a particular cmdlet or function, but my favorite PS thing is object construction:
$foo=""|select bar, baz, gronk|ft
bar baz gronk ----- ----- ------
which is handy for turning unstructured data into a set of objects for further processing.
One of my favorites:
Get-Content "c:\logfile.log" -wait
The powershell-version of 'tail -f 'logfile'
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.