PowerShell Cmdlets
You will notice that for the PowerShell commands I use the word Cmdlet, that is how Microsoft calls and spells the word. In a PowerShell shell you can execute regular windows commands in addition to the cmdlets and most work without any problem some may experience problems depending on the parameters used since PowerShell uses space as a delimiter so do keep this in mind when you are running local exe files.PowerShell cmdlets are in the form of a <verb>-<noun>, you will see common verbs like set, get, clear, write and stop to name a few and each belong to a group of actions, you can get an updated list of verbs at the TechNet site httpss://social.technet.microsoft.com/wiki/contents/articles/4537.powershell-approved-verbs-en-us.aspx do keep this list handy because if you create any module, cmdlet or function you should follow the naming so as to not confuse users and not get warnings from PowerShell when loading modules or cmdlets. To get a list of cmdlets on PS we use the Get-Command cmdlet:PS C:UsersCarlos Perez> lsDirectory: C:UsersCarlos PerezMode LastWriteTime Length Name---- ------------- ------ ----d-r-- 2/16/2012 3:03 PM Contactsd-r-- 3/26/2012 11:23 PM Desktopd-r-- 2/16/2012 3:03 PM Documentsd-r-- 3/8/2012 3:52 PM Downloadsd-r-- 2/16/2012 3:03 PM Favoritesd-r-- 2/16/2012 3:03 PM Linksd-r-- 2/16/2012 3:03 PM Musicd-r-- 2/16/2012 3:03 PM Picturesd-r-- 2/16/2012 3:03 PM Saved Gamesd-r-- 2/16/2012 3:03 PM Searchesd-r-- 2/16/2012 3:03 PM Videos-a--- 3/28/2012 8:16 PM 28 hello.txtPS C:UsersCarlos Perez> cat .hello.txthello worldPS C:UsersCarlos Perez> rm .hello.txtPS C:UsersCarlos Perez> lsDirectory: C:UsersCarlos PerezMode LastWriteTime Length Name---- ------------- ------ ----d-r-- 2/16/2012 3:03 PM Contactsd-r-- 3/26/2012 11:23 PM Desktopd-r-- 2/16/2012 3:03 PM Documentsd-r-- 3/8/2012 3:52 PM Downloadsd-r-- 2/16/2012 3:03 PM Favoritesd-r-- 2/16/2012 3:03 PM Linksd-r-- 2/16/2012 3:03 PM Musicd-r-- 2/16/2012 3:03 PM Picturesd-r-- 2/16/2012 3:03 PM Saved Gamesd-r-- 2/16/2012 3:03 PM Searchesd-r-- 2/16/2012 3:03 PM Videos
PS C:UsersCarlos Perez> Get-Command -Name *service* -CommandType cmdletCommandType Name Definition----------- ---- ----------Cmdlet Get-Service Get-Service [[-Name] <String[]>] [-ComputerName ...Cmdlet New-Service New-Service [-Name] <String> [-BinaryPathName] <...Cmdlet New-WebServiceProxy New-WebServiceProxy [-Uri] <Uri> [[-Class] <Stri...Cmdlet Restart-Service Restart-Service [-Name] <String[]> [-Force] [-Pa...Cmdlet Resume-Service Resume-Service [-Name] <String[]> [-PassThru] [-...Cmdlet Set-Service Set-Service [-Name] <String> [-ComputerName <Str...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] [...
-
Name
-
Synopsis
-
Syntax
-
Description
-
Related Links
-
Remarks
Get-Service [[-Name] <string[]>] [-ComputerName <string[]>] [-DependentServices] [-Exclude <string[]>] [-Include <string[]>] [-RequiredServices] [<CommonParameters>]
PS C:UsersCarlos Perez> Get-Service -Name BITS, VSSStatus Name DisplayName------ ---- -----------Running BITS Background Intelligent Transfer Ser...Stopped VSS Volume Shadow Copy
PS C:UsersCarlos Perez> Get-Service -DisplayName *WMI*Status Name DisplayName------ ---- -----------Stopped wmiApSrv WMI Performance Adapter
| Wildcard Character | Description | Example |
|---|---|---|
|
* |
Matches zero or more characters, starting at the specified position |
a* |
|
? |
Matches any character at the specified position |
?n |
|
[ ] |
Matches a range of characters |
[a-l]name |
|
[ ] |
Matches the specified characters |
[bc]name |
PS C:UsersCarlos Perez> Get-Process -N *vm*Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName------- ------ ----- ----- ----- ------ -- -----------270 20 8956 7708 87 1392 vmtoolsd289 23 15252 14388 145 113.97 2544 vmtoolsd68 9 3444 3148 68 0.55 2532 VMwareTray
PS C:Windowssystem32> Start-Transcript C:windowsTemptesttranscript.txtTranscript started, output file is C:windowsTemptesttranscript.txtPS C:Windowssystem32> Get-Service | select -first 1Status Name DisplayName------ ---- -----------Stopped AeLookupSvc Application ExperiencePS C:Windowssystem32> Get-process | select -first 1Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName------- ------ ----- ----- ----- ------ -- -----------23 4 2128 1460 38 0.09 408 cmdLogName: PS C:Windowssystem32> Stop-TranscriptTranscript stopped, output file is C:windowsTemptesttranscript.txt
PS C:Windowssystem32> $srv = Get-Service -Name BITSPS C:Windowssystem32> $srvStatus Name DisplayName------ ---- -----------Running BITS Background Intelligent Transfer Ser...
PS C:Windowssystem32> $srv.GetType().fullnameSystem.ServiceProcess.ServiceController
PS C:Windowssystem32> Get-Member -InputObject $srvTypeName: System.ServiceProcess.ServiceControllerName MemberType Definition---- ---------- ----------Name AliasProperty Name = ServiceNameRequiredServices AliasProperty RequiredServices = ServicesDependedOnDisposed Event System.EventHandler Disposed(System.Object, System.EventArgs)Close 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 bool Equals(System.Object obj)ExecuteCommand Method System.Void ExecuteCommand(int command)GetHashCode Method int GetHashCode()GetLifetimeService Method System.Object GetLifetimeService()GetType Method type GetType()InitializeLifetimeService Method System.Object InitializeLifetimeService()Pause Method System.Void Pause()Refresh Method System.Void Refresh()Start Method System.Void Start(), System.Void Start(string[] args)Stop Method System.Void Stop()ToString Method string ToString()WaitForStatus Method System.Void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desi...CanPauseAndContinue Property System.Boolean CanPauseAndContinue {get;}CanShutdown Property System.Boolean CanShutdown {get;}CanStop Property System.Boolean CanStop {get;}Container Property System.ComponentModel.IContainer Container {get;}DependentServices Property System.ServiceProcess.ServiceController[] DependentServices {get;}DisplayName Property System.String DisplayName {get;set;}MachineName Property System.String MachineName {get;set;}ServiceHandle Property System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}ServiceName Property System.String ServiceName {get;set;}ServicesDependedOn Property System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}ServiceType Property System.ServiceProcess.ServiceType ServiceType {get;}Site Property System.ComponentModel.ISite Site {get;set;}Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}
PS C:Windowssystem32> (Get-Service -Name BITS).statusStoppedPS C:Windowssystem32> (Get-Service -Name BITS).start()PS C:Windowssystem32> (Get-Service -Name BITS).statusRunning
PS C:Windowssystem32> $srv.Status.GetType()IsPublic IsSerial Name BaseType-------- -------- ---- --------True True ServiceControllerStatus System.Enum
PS C:Windowssystem32> $srv.StatusRunningPS C:Windowssystem32> $srv.Stop()PS C:Windowssystem32> $srv.Refresh()PS C:Windowssystem32> $srv.StatusStopped




