Today some quick tricks finally came together for me with Windows PowerShell. So I can finally write scripts and quickly execute them from the PowerShell shell.
If you haven't yet installed PowerShell, you can find it here: http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx.
My simple goal was to be able to a) write a short script that can take parameters, and b) be able to easily run it from the shell. Here's what I did:
- Close any open PowerShell windows
- Add your script path to the Windows PATH environment variable. I'm using "C:\Users\jkurtz\Documents\WindowsPowerShell", but it can really be anywhere.
- Make sure you have configured PS to run scripts. This page gives the details, but basically do the following:
- Open up a new PowerShell window
- Execute this: Set-ExecutionPolicy RemoteSigned
- Create a simple script, named with a "ps1" extension, in the folder specified in step (2) above. I've included a sample script below
- Now simply enter the name of the script at the PowerShell command-line (including any required arguments) and hit Enter
If you want to try it out, but don't have a script in mind, you can use this one. It's not the most useful script in the world, but it works for this example. Just use Notepad to create a file in your script path called "members.ps1", and paste the following contents into it.
param (
[string] $domainname = $(throw 'please specify a domain name (can use . for local machine)'),
[string] $groupname = $(throw 'please specify a group name')
)
$group =[ADSI]("WinNT://" + $domainname + "/" + $groupname)
$members = @($group.psbase.Invoke("Members"))
$members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
|
Then, if you've followed the 5 steps above, you can simply enter the following at the PowerShell command-line:
members -domainname DOMAIN -groupname "domain admins"
Or, leave out the -domainname and -groupname switches:
members DOMAIN "domain admins"
For a reference and guides to using PowerShell, here's some great resources:
Note that version 2.0 of PowerShell is coming soon.