16

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

Jon Galloway
  • 1,506
  • 1
  • 17
  • 20

13 Answers13

13

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.

Steven Murawski
  • 1,580
  • 3
  • 14
  • 25
5

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()
Eldila
  • 255
  • 2
  • 5
  • 2
    This can be done in one line with the Quest AD cmdlets - they're free and super useful! http://www.quest.com/activeroles-server/arms.aspx – Doug Chase May 15 '09 at 13:20
  • Also see http://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 06 '12 at 08:13
3

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.

Marcus
  • 536
  • 1
  • 3
  • 9
2

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

2

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

Brandan
  • 29
  • 2
  • 8
1

One of my favorites:

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

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

  • 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 02 '10 at 18:24
1

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:\
dance2die
  • 2,011
  • 7
  • 32
  • 41
1

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.

Chris Magnuson
  • 3,771
  • 10
  • 42
  • 46
1

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.

aleksandar
  • 319
  • 2
  • 2
1

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.

user2278
  • 873
  • 5
  • 9
1

For me -WhatIf is the most useful.

travis
  • 151
  • 2
  • 8
0

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.

Ivo Looser
  • 311
  • 1
  • 4
0

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.

Andy Schneider
  • 1,543
  • 5
  • 19
  • 28