At Tech Ed in Orlando last week John Waters picked up "Windows PowerShell Unleashed by Tyson Kopczynski" and as John has a eye for cool technology I did likewise and got hooked on PowerShell. I'm not usually a live-on-the-command-line-love-batch-files kind of guy but PowerShell covers enough ground in a powerful and consistent way that I'm considering adding this to my technology toolbox. Although PowerShell is intended for system administrator use, there may be a place for PowerShell in development to do limited testing against .NET objects, task automatation and general exploration. In any case it's a great toy and a hoot to play with.
PowerShell has several important differences from cmd.exe.
Here's a sample session of PowerShell to give you a very brief notion of how it works. Be aware that this is only scratching the surface of the possibilities for PowerShell. Let's say we want to work with Windows services, so we need to know what commands are available:
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> get-command *service
CommandType Name Definition----------- ---- ----------Cmdlet Get-Service Get-Service [[-Name] <String[]>] [-Include <Stri...Cmdlet New-Service New-Service [-Name] <String> [-BinaryPathName] <...Cmdlet Restart-Service Restart-Service [-Name] <String[]> [-Force] [-Pa...Cmdlet Resume-Service Resume-Service [-Name] <String[]> [-PassThru] [-...Cmdlet Set-Service Set-Service [-Name] <String> [-DisplayName <Stri...Cmdlet Start-Service Start-Service [-Name] <String[]> [-PassThru] [-I...Cmdlet Stop-Service Stop-Service [-Name] <String[]> [-Force] [-PassT...Cmdlet Suspend-Service Suspend-Service [-Name] <String[]> [-PassThru] [...
From here we can see what services are available for SQL Server:
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> get-service MSSQL*
Status Name DisplayName------ ---- -----------Running MSSQL$NRLAPTOP2 MSSQL$NRLAPTOP2Running MSSQL$SQLEXPRESS SQL Server (SQLEXPRESS)Running MSSQL$TELERIK MSSQL$TELERIKRunning MSSQLSERVER SQL Server (MSSQLSERVER)Stopped MSSQLServerADHe... SQL Server Active Directory HelperRunning MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
Now we want to stop the MSSQLSERVER service and any dependant services:
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> stop-service "MSSQLSERVER" -force
If we re-run get-service we can see that the service is stopped:
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> get-service mssql*
Status Name DisplayName------ ---- -----------Running MSSQL$NRLAPTOP2 MSSQL$NRLAPTOP2Running MSSQL$SQLEXPRESS SQL Server (SQLEXPRESS)Running MSSQL$TELERIK MSSQL$TELERIKStopped MSSQLSERVER SQL Server (MSSQLSERVER)Stopped MSSQLServerADHe... SQL Server Active Directory HelperRunning MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
You can also interrogate the service objects using the get-member command. For example you could take the service objects returned by get-service and direct them to the get-member command using the "|" pipe symbol. The following is only a partial listing.
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> get-service | get-member
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition---- ---------- ----------Name AliasProperty Name = ServiceNameClose Method System.Void Close()Continue Method System.Void Continue()CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)Dispose Method System.Void Dispose()Equals Method System.Boolean Equals(Object obj)ExecuteCommand Method System.Void ExecuteCommand(Int32 command)get_DependentServices Method System.ServiceProcess.ServiceController[] get_DependentServices()get_DisplayName Method System.String get_DisplayName()get_MachineName Method System.String get_MachineName()get_ServiceHandle Method System.Runtime.InteropServices.SafeHandle get_ServiceHandle()
What about navigation? If I want to change locations in the file system of course there's "CD" or the PS native "set-location". What's unique here is that you can navigate the registry, environmental variables, certificate stores or any other system that PS has a provider for (yes, you can write your own providers). For example the following is perfectly legal:
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> cd env:PS Env:\> dir
Name Value---- -----Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technolo...TEMP C:\DOCUME~1\NOELRI~1\LOCALS~1\TempSESSIONNAME RDP-Tcp#1PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1USERDOMAIN NRLAPTOPPROCESSOR_ARCHITECTURE x86
You could image using CD to navigate hiearchical database information in this way (to what practical end I do not know, but it is amusing). Find out what PowerShell drives are available on your machine by using get-psdrive:
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> get-psdrive
Name Provider Root CurrentLocation---- -------- ---- ---------------Alias AliasC FileSystem C:\ Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debugcert Certificate \D FileSystem D:\Env EnvironmentFunction FunctionHKCU Registry HKEY_CURRENT_USERHKLM Registry HKEY_LOCAL_MACHINEVariable Variable
Use get-psprovider to find the providers on your system. See the MSDN for examples of writing your own provider.
PS C:\Clients\Falafel\Projects\FalafelCmdletLibrary\bin\Debug> Get-PSProvider
Name Capabilities Drives---- ------------ ------Alias ShouldProcess {Alias}Environment ShouldProcess {Env}FileSystem Filter, ShouldProcess {C, D}Function ShouldProcess {Function}Registry ShouldProcess {HKLM, HKCU}Variable ShouldProcess {Variable}Certificate ShouldProcess {cert}
You can download PowerShell at http://www.microsoft.com/technet/scriptcenter/topics/msh/download.mspx. It comes with the install and docs for "Getting Started", "Quick Reference" and "Users Guide".
Remember Me
a@href@title, i, strike, u
Copyright © 2003-2008 Falafel Software Inc.
Subscribe to Falafel Blogs
The opinions expressed herein are Falafel's employees own personal opinions and do not represent Falafel Software's view in any way in case they go bananas!