Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What's your top Powershell command? Let's make a list and vote up the best ones!

share|improve this question
    
@Jon: did you mean "cmdlet"? –  Sung Apr 30 '09 at 23:39
    
Should be tagged as subjective –  halr9000 Jun 4 '09 at 13:32
add comment

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.

13 Answers

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.

share|improve this answer
add comment

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()
share|improve this answer
2  
This can be done in one line with the Quest AD cmdlets - they're free and super useful! quest.com/activeroles-server/arms.aspx –  Doug Chase May 15 '09 at 13:20
    
Also see blogs.msdn.com/adpowershell/default.aspx! –  Doug Chase May 15 '09 at 13:21
1  
@Doug Chase Yes, it's free, but some organizations won't let you use unapproved software, with really bad lead times for getting stuff approved, but scripts you write yourself are instantly available. –  Orihara May 15 '09 at 20:46
1  
In case anyone sees this now and thinks this is a good script, if you have the RSAT tools installed, you can do this with a one-liner in Powershell 2.0: Get-ADUser -filter 'Name -like "*"' (replace "filter" with whatever) –  Mark Henderson Mar 6 '12 at 8:13
add comment

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.

share|improve this answer
add comment

At the moment I'm a big fan of Get-WMIObject.

share|improve this answer
add comment

"Test-Path" finding this useful for checking if data is already there or if a default needs to be added.

share|improve this answer
add comment

For me -WhatIf is the most useful.

share|improve this answer
    
-1: -Whatif is not a cmdlet but a cmdlet option. –  Sung May 7 '09 at 4:47
1  
he just specified "command" not cmdlet –  travis May 19 '09 at 15:05
add comment

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)

cd HKLM:\
share|improve this answer
add comment

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.

share|improve this answer
add comment

One of my favorites:

Get-Content "c:\logfile.log" -wait

The powershell-version of 'tail -f 'logfile'

share|improve this answer
1  
This doesn't seem to work for me. I ran this command against a CSV file that another powershell command was writing data to and it did not display the updates to the file as it was being changed but only showed me the lines of the file as it existed when I ran the command and then just hung waiting for CTRL+Break. –  Chris Magnuson Nov 2 '10 at 18:24
add comment

I see Get-Member listed which I have to agree is the best but a near and equally neccessary second for me is:

Format-List

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.

share|improve this answer
add comment

I did just a small method that i added to my Profile. See http://blog.keystroke.ch/2007/08/30/diskusage-in-powershell/.

diskusage myserver

is my prefered command.

share|improve this answer
add comment

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

gives you

bar   baz   gronk
----- ----- ------

which is handy for turning unstructured data into a set of objects for further processing.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.