Many developers love PowerShell, and for good reason: it adds power, functionality, and flexibility to the Windows Command Prompt, where many of us spend a good deal of time. It does, however, come with a bit of a learning curve, but once you’ve mastered the essential commands you need to know, it’s productivity on steroids.
PowerShell commands are known as cmdlets, and these cmdlets are the driving force behind its functional capabilities. From commands that improve the overall Windows experience to commands useful for development work, there are dozens of important commands developers should know. We’ve put together this list to serve as a handy reference guide for those who are just beginning to tap into the power of PowerShell as well as those who want to level-up their PowerShell experience, including:
In addition to cmdlets, there are dozens of parameters and methods that help you get the most out of your scripts. The WhatIf parameter is particularly useful for testing PowerShell scripts without actually running them (@pluralsight). There are typically several parameters and methods available for each command. The commands listed below are a good starting point for any developer, but to fully realize the benefits, you should master parameters and other methods as well.
These basic PowerShell commands are helpful for getting information in various formats, configuring security, and basic reporting.
Get-Command is an easy-to-use reference cmdlet that brings up all the commands available for use in your current session.
Simply type in this command:
get-command
The output will look something like this (@MS_ITPro):
CommandType Name Definition
----------- ---- ----------
Cmdlet Add-Content Add-Content [-Path] <String[...
Cmdlet Add-History Add-History [[-InputObject] ...
Cmdlet Add-Member Add-Member [-MemberType]
The Get-Help command is essential for anyone using PowerShell, providing quick access to the information you need to run and work with all of the available commands.
If you wanted some examples, for instance, you’d enter the following (@jp_jofre):
Get-Help [[-Name] <String>] [-Path <String>] [-Category <String[]>] [-Component <String[]>]
[-Functionality <String[]>] [-Role <String[]>] [-Examples] [<CommonParameters>]
Microsoft disables scripting by default to prevent malicious scripts from executing in the PowerShell environment. Developers want to be able to write and execute scripts, however, so the Set-ExecutionPolicy command enables you to control the level of security surrounding PowerShell scripts. You can set one of four security levels:
Similarly, if you’re working in an unfamiliar environment, you can easily find out what the current execution policy is using this command:
Get-ExecutionPolicy
Check out this thread on SuperUser for more information on configuring the execution policy in PowerShell. (@StackExchange)
It’s also helpful to know what services are installed on the system. You can easily access this information with the following command:
Get-Service
The output will look something like the following (@MS_ITPro):
Status Name DisplayName
------ ---- -----------
Running AdobeActiveFile... Adobe Active File Monitor V4
Stopped Alerter Alerter
Running ALG Application Layer Gateway Service
Stopped AppMgmt Application Management
Running ASChannel Local Communication Channel
If you need to know if a specific service is installed, you can append the -Name switch and the name of the service, and Windows will show the state of the service. Additionally, you can leverage filtering capabilities to return a specific subset of currently installed services. The following example will result in an output of data from the Get-Service command that’s been piped to the Where-Object cmdlet, which then filters out everything other than the services that have been stopped:
Get-Service | Where-Object {$_.status -eq "stopped"}
Check out this post for some additional examples of Get-Service in action.
If you need to extract data that you can use in a report or send to someone else, the ConvertTo-HTML is one simple way to do so. To use it, pipe the output from another command to the ConvertTo-HTML command and use the -Property switch to specify which output properties you want in the HTML file. You’ll also need to provide a file name.
For example, the following code creates an HTML page that lists the PowerShell aliases in the current console:
PS C:\> get-alias | convertto-html > aliases.htm
PS C:\> invoke-item aliases.htm
Bonus: The
Export-CSV
cmdlet functions in much the same way, but exports data to a .CSV file rather than HTML. Use
Select-Object
to specify which properties you want to be included in the output.
Check out this article from 4sysops for more information on using ConvertTo-HTML effectively (@adbertram).
You can actually use PowerShell to parse your machine’s event logs using the Get-EventLog cmdlet. There are several parameters available. Use the -Log switch followed by the name of the log file to view a specific log. You’d use the following command, for example, to view the Application log:
Get-EventLog -Log "Application"
Check out a few more examples of Get-EventLog in action in this post (@nextofwindows). Other common parameters include (@SS64):
-Verbose
-Debug
-ErrorAction
-ErrorVariable
-WarningAction
-WarningVariable
-OutBuffer
-OutVariable
Much like getting a list of available services, it’s often useful to be able to get a quick list of all the currently running processes. The Get-Process command puts this information at your fingertips.
Bonus: Use Stop-Process to stop processes that are frozen or is no longer responding. If you’re not sure what process is holding you up, use Get-Process to quickly identify the problematic process. Once you have the name or process ID, use Stop-Process to terminate it.
Here’s an example. Run this command to terminate all currently running instances of Notepad (@MS_ITPro):
Stop-Process -processname notepad
You can use wildcard characters, too, such as the following example which terminates all instances of Notepad as well as any other processes beginning with note:
Stop-Process -processname note*
Check out this post for more information on killing processes with PowerShell (@howtogeek):
What if you want to clear the entries from your command history? Easy – use the Clear-History cmdlet. You can also use it to delete only specific commands. For example, the following command would delete commands that include “help” or end in “command” (@MS_ITPro):
PS C:\> Clear-History -Command *help*, *command
If you want to add entries to a session, use:
Add-History
Check out this post for some useful information on clearing the history and pre-loading the history with a list of certain commands (@MS_ITPro).
This cmdlet is used to format any output as a table. For example, the following command gets running processings, sorts them based on the BasePriority, and formats the list in a table:
Get-Process | Format-Table -Property Name, CPU, Memory -AutoSize
The example above produces a table format like the following:
BasePriority: 1
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
857 15 7248 8612 63.98 936 0 services
1824 30 11292 13316 49.44 964 0 lsass
BasePriority: 2
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
640 22 67352 6336 83.28 17108 1 Docker Desktop
1228 52 336352 186876 9,442.59 10336 1 chrome
BasePriority: 3
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
53 3 1096 604 0.33 420 0 smss
Format-List lets output properties as a list, each on a new line. For example, the following command gets services and lists each individually using Format-List cmdlet:
Get-Service | Format-List
Name : AarSvc_5e1b8
DisplayName : AarSvc_5e1b8
Status : Stopped
DependentServices : {}
ServicesDependedOn : {}
CanPauseAndContinue : False
CanShutdown : False
CanStop : False
ServiceType : 224
Name : Appinfo
DisplayName : Application Information
Status : Running
DependentServices : {}
ServicesDependedOn : {RpcSs, ProfSvc}
CanPauseAndContinue : False
CanShutdown : False
CanStop : True
ServiceType : Win32OwnProcess, Win32ShareProcess
Where-Object is one of the most important cmdlets to know, as it enables you to take a dataset and pass it further down your pipeline for filtering (@jonathanmedd):
Get-Service | Where-Object {$_.Status -eq 'Running'}
Status Name DisplayName
------ ---- -----------
Running AdobeARMservice Adobe Acrobat Update Service
Running AppHostSvc Application Host Helper Service
Running Appinfo Application Information
Running AudioEndpointBu... Windows Audio Endpoint Builder
Running Audiosrv Windows Audio
Running BFE Base Filtering Engine
Running BITS Background Intelligent Transfer Ser...
Running BrokerInfrastru... Background Tasks Infrastructure Ser...
Running Browser Computer Browser
Running CDPSvc Connected Devices Platform Service
If you want to keep your work secure in production and prevent modification, use Set-AuthenticodeSignature to add an Authenticode signature to a script or file.
> Set-AuthenticodeSignature somescript.ps1 @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0] -IncludeChain "All" -TimestampServer "http://timestamp.verisign.com/scripts/timstamp.dll"
When it comes to productivity, PowerShell can help you get things done with the following commands.
The ForEach-Object cmdlet performs an operation against every item in a specified group of input objects. While many cmdlets work with every object in a collection anyway, you’ll need ForEach-Object for those situations in which you want to make other modifications or apply specific formatting to all objects in a collection.
Here’s an example (@MS_ITPro). If you want to display a list of process names and want those names to render in cyan, you might try the following:
Get-Process | Write-Host $_.name -foregroundcolor cyan
But the above will produce the following error:
At line:1 char:25
+ get-process | write-host <<<< $_.name -foregroundcolor cyan Write-Host : The input object
cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters
that take pipeline input.
Because the Write-Host cmdlet doesn’t understand what you want to do with the data that’s sent over the pipeline.
So, using the ForEach-Object cmdlet solves this problem:
Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}
Check out this tutorial for more information on ForEach-Object and working with loops (@tomsitpro).
If you want to delete the contents of an item but retain the item itself, you’ll use the Clear-Content cmdlet:
Clear-Content C:\Temp\TestFile.txt
You can also use this command to clear the contents of all files with a specified file extension. The following code would clear the contents of all files with the .txt extension, for instance:
Clear-Content -path * -filter *.TXT –force
You can also use wildcard characters. Plus, you can clear the contents of any type of file, from .txt files to .doc, .xls, and more.
Check out this post for more details.
If you’re making major changes or running a risky experiment, you can set a restore point on your machine with the Checkpoint-Computer cmdlet.
Note that you can only create a restore point using this cmdlet once every 24 hours. If you run the command again, it will keep the previous restore point:
PS C:\> Checkpoint-Computer -Description "My 2nd checkpoint" -RestorePointType "Modify_Settings"
PS C:\> Get-ComputerRestorePoint | format-list
__GENUS : 2
__CLASS : SystemRestore
__SUPERCLASS :
__DYNASTY : SystemRestore
__RELPATH : SystemRestore.SequenceNumber=59
__PROPERTY_COUNT : 5
__DERIVATION : {}
__SERVER : CLIENT2
__NAMESPACE : root\default
__PATH : \\CLIENT2\root\default:SystemRestore.SequenceNumber=59
CreationTime : 20120202180537.316029-000
Description : My 2nd checkpoint
EventType : 100
RestorePointType : 12
SequenceNumber : 59
Check out this article from MCP Mag for more (@MCPmag).
It’s often useful to be able to compare two objects directly. You can do this using Compare-Object, which generates a report on the differences between two sets such as (@Marcam923):
PS G:\lee\tools> cd c:\temp
PS C:\temp> $set1 = "A","B","C"
PS C:\temp> $set2 = "C","D","E"
PS C:\temp> Compare-Object $set1 $set2
InputObject SideIndicator
----------- -------------
D =>
E =>
A <=
B <=
Use ConvertFrom-StringData to convert a string containing one or more value pairs to a hash table. Here’s an example of what the command looks like:
$settings = $TextData | ConvertFrom-StringData
This command is useful in a variety of situations, such as when you want to save the settings for a PowerShell script to enable others to edit the settings without working in the script code directly.
Convert an encrypted standard string to a secure string or plain text to a secure string using ConvertTo-SecureString. This cmdlet is used in conjunction with ConvertFrom-SecureString and Read-Host (@AdmArsenal):
ConvertTo-SecureString [-String] SomeString
ConvertTo-SecureString [-String] SomeString [-SecureKey SecureString] ConvertTo-SecureString [-String] SomeString [-Key Byte[]] ConvertTo-SecureString [-String] SomeString [-AsPlainText] [-Force]
Use the ConvertTo-XML cmdlet to create an XML-based representation of an object. This is also called serialization, and it’s a useful process for saving data for later re-use. Note that it’s important that your expression writes objects to the pipeline. Anything using Write-Host won’t write to the pipeline and therefore can’t be serialized. Here’s an example of ConvertTo-XML in action (@PetriFeed):
Get-Service wuauserv -ComputerName chi-dc04,chi-p50,chi-core01 |
Export-Clixml -Path c:\work\wu.xml
The specific cmdlet used in the above example, Export-Clixml, is suitable for most purposes. It converts the output of a PowerShell expression to XML and saves it to a file.
New-AppLockerPolicy creates a new AppLocker policy from a list of file information and other rule creation options. In total, there are five cmdlets that enable you to interact with AppLocker, including (@RootUsers_):
New-ItemProperty creates a new property for an item and sets its value. You can use it to create and change registry values and data (properties of a registry key), for instance.
Check out this tutorial from mnaoumov.NET for some useful workarounds using this cmdlet (@mnaoumov).
To create an instance of a Microsoft .NET Framework or Component Object Model (COM) object, use the New-Object cmdlet.
Here’s an example that creates a new object using New-Object, stores it in a variable, then pipes it to Add-Member, which will then add properties or methods specified in the object created (@gngrninja):
$ourObject = New-Object -TypeName psobject
$ourObject | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computerInfo.Name
$ourObject | Add-Member -MemberType NoteProperty -Name OS -Value $osInfo.Caption
$ourObject | Add-Member -MemberType NoteProperty -Name 'OS Version' -Value $("$($osInfo.Version) Build $($osInfo.BuildNumber)")
$ourObject | Add-Member -MemberType NoteProperty -Name Domain -Value $computerInfo.Domain
$ourObject | Add-Member -MemberType NoteProperty -Name Workgroup -Value $computerInfo.Workgroup
$ourObject | Add-Member -MemberType NoteProperty -Name DomainJoined -Value $computerInfo.Workgroup
$ourObject | Add-Member -MemberType NoteProperty -Name Disks -Value $diskInfo
$ourObject | Add-Member -MemberType NoteProperty -Name AdminPasswordStatus -Value $adminPasswordStatus
$ourObject | Add-Member -MemberType NoteProperty -Name ThermalState -Value $thermalState
New-WebServiceProxy creates a web service proxy object that enables you to use and manage the web service from within PowerShell. This cmdlet is a beautiful thing for developers – it makes it unnecessary to write a lot of complex code to try to accomplish something in PowerShell when you can simply call another service that already makes it possible.
Here’s an example:
$url = http://<webapp>.azurewebsites.net/CreateSite.asmx
$proxy = New-WebServiceProxy $url
$spAccount = "<username>"
$spPassword = Read-Host -Prompt "Enter password" –AsSecureString
$projectGuid = ""
$createOneNote = $false
Now, you can run the following to view a list of all available methods:
$proxy | gm -memberType Method
Similarly to New-WebServiceProxy, New-WSManInstance creates a new instance of a management resource.
New-WSManInstance winrm/config/Listener
-SelectorSet @{Address="*";Transport="HTTPS"}
-ValueSet @{Hostname="Test01";CertificateThumbprint="01F7EB07A4531750D920CE6A588BF5"}
Check out this tutorial for a complete step-by-step example of how to get the information you need to execute this script successfully (@jonathanmedd).
New-WSManSessionOption creates a new management session hash table that’s used as input parameters to other WS-Management cmdlets including:
Here’s the syntax (@SAPIENTech):
New-WSManSessionOption [-NoEncryption] [-OperationTimeout] [-ProxyAccessType] [-ProxyAuthentication] [-ProxyCredential] [-SkipCACheck] [-SkipCNCheck] [-SkipRevocationCheck] [-SPNPort] [-UseUTF16] [<CommonParameters>]
The Select-Object cmdlet selects the specified properties of a single object or group of objects. Additionally, it can select unique objects from an array or a specified number of objects from the beginning or end of an array.
PS > Get-Process | Sort-Object name -Descending | Select-Object -Index 0,1,2,3,4
This tutorial provides more information about the various ways you can use Select-Object (@infosectactico).
There are other cmdlets with similar functions including:
Set-Alias is a great command for enhancing productivity. It allows you to set an alias for a cmdlet or other command element in the current session (similar to a keyboard shortcut) so you can work faster.
The following example sets Notepad to np in the current session using Set-Alias (@powershellatoms):
New-Alias np c:\windows\system32\notepad.exe
Note that you can also customize your PowerShell profile with the aliases you use most often (@howtogeek).
Set-StrictMode establishes and enforces coding rules in scripts, script blocks, and expressions. It’s a useful command for enforcing code quality and preventing you from slacking off and writing sloppy code when it’s 3:00 a.m. and you haven’t had any sleep in two days (@adbertram).
To use it, there are two parameters to consider: -Off and -Version, and -Version has three possible values:
Wait-Job suppresses the command prompt until background jobs running in the current session are complete. Wait-Job doesn’t show the output from jobs, however, but it can be used in conjunction with Receive-Job. Multithreading is possible in PowerShell thanks to -Jobs.
### Start-MultiThread.ps1 ###
$Computers = @("Computer1","Computer2","Computer3")
#Start all jobs
ForEach($Computer in $Computers){
Start-Job -FilePath c:ScriptGet-OperatingSystem.ps1 -ArgumentList $Computer
}
#Wait for all jobs
Get-Job | Wait-Job
#Get all job results
Get-Job | Receive-Job | Out-GridView
1
2
3
4
5
6
7
8
9
10
11
12
13
### Start-MultiThread.ps1 ###
$Computers = @("Computer1","Computer2","Computer3")
#Start all jobs
ForEach($Computer in $Computers){
Start-Job -FilePath c:ScriptGet-OperatingSystem.ps1 -ArgumentList $Computer
}
#Wait for all jobs
Get-Job | Wait-Job
#Get all job results
Get-Job | Receive-Job | Out-GridView
If you need to save cmdlet output to a file, use Out-File, which creates a local file.
This example uses Get-Process and formats the output in ASCII format, then Out-File uses FilePath to create a Process.txt containing the output.
$Procs = Get-Process
Out-File -FilePath .\Process.txt -InputObject $Procs -Encoding ASCII -Widt
Who doesn’t love a status bar? Monitor your progress using Write-Progress, which displays a progress bar within a Windows PowerShell command window.
Here’s an example that gives you a full progress bar and runtime strings (@credera):
$TotalSteps = 4
$Step = 1
$StepText = "Setting Initial Variables"
$StatusText = '"Step $($Step.ToString().PadLeft($TotalSteps.Count.ToString().Length)) of $TotalSteps | $StepText"'
$StatusBlock = [ScriptBlock]::Create($StatusText)
$Task = "Creating Progress Bar Script Block for Groups"
Write-Progress -Id $Id -Activity $Activity -Status (& $StatusBlock) -CurrentOperation $Task -PercentComplete ($Step / $TotalSteps * 100)
There are also a variety of cmdlets useful for developers for troubleshooting, testing, debugging, and monitoring purposes. Here are a few you need to know.
Developers love debugging! Well, we like it even more when there are no bugs to eliminate, but sadly that’s not always the case. With PowerShell, you can debug a process using the Debug-Process cmdlet.
You can also debug jobs using Debug-Job (@MS_ITPro). And, you can set breakpoints or use the Wait-Debugger cmdlet:
PS C:\> $job = Start-Job -ScriptBlock { Set-PSBreakpoint C:\DebugDemos\MyJobDemo1.ps1 -Line 8; C:\DebugDemos\MyJobDemo1.ps1 }
PS C:\> $job
PS C:\> Debug-Job $job
If you have at one time set breakpoints but want to eliminate them, do so easily using Disable-PSBreakpoint, which disables breakpoints in the current console. Here’s the syntax (@ActiveXperts):
Disable-PSBreakpoint [-Breakpoint] [-PassThru] [-Confirm] [-WhatIf] []
Disable-PSBreakpoint [-Id] [-PassThru] [-Confirm] [-WhatIf] []
Alternatively, if you want to enable breakpoints in the current console, use Enable-PSBreakpoint.
Get-Counter gets real-time performance counter data from the performance monitoring instrumentation in Windows OS. It’s used to get performance data from local or remote computers at specific sample intervals that you specify.
In this example, you’ll get a counter set with a sample interval for a specified maximum sample (@MS_ITPro):
PS C:\> Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 3
In the example below, this command gets specific counter data from multiple computers:
The first command saves the **Disk Reads/sec** counter path in the $DiskReads variable.
PS C:\> $DiskReads = "\LogicalDisk(C:)\Disk Reads/sec"
The second command uses a pipeline operator (|) to send the counter path in the $DiskReads variable to the **Get-Counter** cmdlet. The command uses the **MaxSamples** parameter to limit the output to 10 samples.
PS C:\> $DiskReads | Get-Counter -Computer Server01, Server02 -MaxSamples 10
Export-Counter exports PerformanceCounterSampleSet objects as counter log files. Two properties are available:
And several methods, all of which are inherited from Object:
For example, the following command uses Get-Counter to collect Processor Time data and exports it to a .blg file using Export-Counter (@TechGenix):
Get-Counter "\Processor(*)\% Processor Time" | Export-Counter -Path C:\Temp\PerfData.blg
Test-Path lets you verify whether items exist in a specified path. For instance, if you’re planning to use another command on a specified file, you may need to verify that the file exists to avoid throwing an error.
Test-Path C:\Scripts\Archive
If the folder exists, it will return True; if it doesn’t, it will return False.
It can also work with the paths used by other PowerShell providers. For instance, if you need to know if your computer has an environment variable called username, you could use the following:
Test-Path Env:\username
Test-Path works with variables, certificates, aliases, and functions. Check out this post from TechNet for more details (@MS_ITPro).
Look at Windows event logs using Get-WinEvent. For a list of available logs, use:
Get-WinEvent -ListLog *
Then, to review the details of a specific log, replace * with the name (pipe the output to format-list to view all the details):
Get-WinEvent -ListLog $logname | fl *
You can also view all the events in a log by using:
Get-WinEvent -LogName System
Check out this tutorial for more details (@rakheshster).
Troubleshooting packs are collections of PowerShell scripts and assemblies that help you troubleshoot, diagnose, and repair common system problems (@ITNinjaSite). Find troubleshooting packs at:
C:\Windows\Diagnostics\System
You can run this script to get a list of all the troubleshooting packs available on the current system (@TechGenix):
Get-ChildItem C:\Windows\Diagnostic\System
Then, from an elevated PowerShell window, run a troubleshooting pack using this command:
Invoke-TroubleshootingPack (Get-TroubleshootingPack C:\Windows\diagnostics\system\networking)
If you want to time operations in PowerShell, Measure-Command is a must-know cmdlet. It measures how long a script or scriptblock to run. Here’s an example (@ToddKlindt):
Measure-Command { Mount-SPContentDatabase –Name wss_content_portal –WebApplication http://portal.contoso.com }
The output is a TimeSpan object, so it contains properties such as Hour, Minute, Second, etc., and it’s easy to tailor the output to your preferences.
You might also want to know how large a given object is. Use Measure-Object to calculate the numeric properties of any object, including characters, words, and lines in a string object, such as files of text.
Just specify the name and the type of measurement to perform, along with parameters such as (@WindowsITPro):
The following command sums the VirtualMemorySize property values for all process objects:
Get-Process | measure VirtualMemorySize -Sum
New-Event is used to create a new event. A related cmdlet is New-EventLog, which creates a new event log as well as a new event source on a local or remote computer. If you have an automation engine supported by PowerShell, it’s a good practice to set up an event log (by creating a custom Event Log Type) that logs all messages sent by PowerShell. This is one example where you can implement Custom Logging in Event Viewer.
Start by creating a new Event Log LogName (@BundaloVladimir):
New-EventLog -LogName Troubleshooting_Log -Source FalloutApp
Then, to send messages to your new event log, run the following using the Write-Log cmdlet:
Write-EventLog -log Troubleshooting_Log -source FalloutApp -EntryType Information -eventID 10 -Message "FalloutApp has been successfully installed"
If you need to get the results of Windows PowerShell background jobs in the current session, use Receive-Job. This is usually used after using Start-Job to begin a job when you need to view the specific results.
Receive-Job -Name HighMemProcess
Check out this helpful tutorial on using Receive-Job and what to do if there are seemingly no results (@proxb).
This cmdlet is used to subscribe to the events generated by the Windows PowerShell engine and the New-Event cmdlet. For example, the following command subscribes to an event when the current PowerShell session exits and saves information (such as date and time) to a log file (@jonathanmedd):
Register-EngineEvent PowerShell.Exiting
-Action {"PowerShell exited at " + (Get-Date) | Out-File c:\log.txt -Append}
Register-ObjectEvent is similar to Register-EngineEvent, but rather than subscribe to events generated by the PowerShell engine and New-Event, it subscribes to the events generated by a Microsoft .NET Framework Object.
Here’s an example (@NetworkWorld):
Register-ObjectEvent -InputObject $MyObject -EventName OnTransferProgress -SourceIdentifier Scp.OnTransferProgress `
-Action {$Global:MCDPtotalBytes = $args[3]; $Global:MCDPtransferredBytes = $args[2]}
Register-ObjectEvent -InputObject $MyObject -EventName OnTransferEnd `
-SourceIdentifier Scp.OnTransferEnd -Action {$Global:MCDPGetDone = $True}
There are other Register- cmdlets that you may find useful including:
When you want to remove an event, use the Remove-Event cmdlet. If you need to remove an entire event log, you’d use Remove-EventLog, which deletes an event log or unregisters an event source.
Alternatively, Unregister-Event cancels an event subscription but does not delete an event from the event queue (@SS64).
This cmdlet turns script debugging features on and off. It also sets the trace level and toggles StrictMode.
By using Set-PSDebug at the top of your script file just after the param() statement (if any), you can prevent errors with scripts that PowerShell doesn’t provide adequate information about for troubleshooting purposes. Here’s an example (@r_keith_hill):
Set-PSDebug -Strict
$Suceeded = test-path C:\ProjectX\Src\BuiltComponents\Release\app.exe
if ($Succeded) {
"yeah"
}
else {
"doh"
}
PS C:\Temp> .\foo.ps1
The variable $Succeded cannot be retrieved because it has not been set yet.
At C:\Temp\foo.ps1:6 char:14
+ if ($Succeded) <<<< {
If you need to suspend the activity in a script or session, use Start-Sleep, which halts the activity for a specified time period.
Start-Sleep -Seconds xxx Start-Sleep -Milliseconds xxx
If you need to pause one or more running services, you’d use Suspend-Service.
If you’re analyzing performance or code quality, it’s useful to be able to view the output of a command. Tee-Object stores the command output in a file or variable and also displays it in the console if it’s the last variable in the pipeline. If it’s not the last variable in the pipeline, Tee-Object sends it down the pipeline.
Here’s the syntax:
Tee-Object [-FilePath] <string> [-InputObject <psobject>] [<CommonParameters>]
Tee-Object -Variable <string> [-InputObject <psobject>] [<CommonParameters>]
Test-AppLockerPolicy evaluates whether input files are permitted to run for a specific user based on the specified AppLocker policy.
Test-AppLockerPolicy [-PolicyObject] -Path [-User ] [-Filter >] []
Test-AppLockerPolicy [-XMLPolicy] -Path [-User ] [-Filter ] [ arameters>]
This tutorial offers more details about the available parameters and examples of Test-AppLockerPolicy in action (@powershellhelp).
This cmdlet tests and repairs the connection between a local computer and its domain. Without this command, the usual solution was previously to remove a computer from its domain and then reconnect it in order to reestablish the relationship. Test-ComputerSecureChannel makes it possible to reestablish the connection in less time (@WindowsITPro).
When signed on as a local administrator, simply run the following:
Test-ComputerSecureChannel –credential WINDOWSITPRO\Administrator –Repair
You can use Test-Connection to send Internet Control Message Protocol (ICMP) echo request packets (pings) to one or more computers.
Use Test-Path to determine whether all elements of a path exist. Essentially, it helps you handle errors before they occur. In its simplest form, it returns True or False (@MCPmag):
PS C:\> test-path c:\
True
PS C:\> test-path z:\foo
False
Trace-Command configures and starts the trace of a specified command or expression. To use it, you’ll also need to use Get-TraceSource in order to look for particular names using wildcard characters:
PS> Get-TraceSource -Name *param*
You can filter the output to match the description to the pattern you’re after. Once you’ve identified the possible trace name, you’ll use Trace-Command to get the answers you need. Here’s an example:
[CmdletBinding(DefaultParameterSetName = 'Host')]
param (
# ScriptBlock that will be traced.
[Parameter(
ValueFromPipeline = $true,
Mandatory = $true,
HelpMessage = 'Expression to be traced'
)]
[ScriptBlock]$Expression,
# Name of the Trace Source(s) to be traced.
[Parameter(
Mandatory = $true,
HelpMessage = 'Name of trace, see Get-TraceSource for valid values'
)]
[ValidateScript({
Get-TraceSource -Name $_ -ErrorAction Stop
})]
[string[]]$Name,
# Option to leave only trace information
# without actual expression results.
[switch]$Quiet,
# Path to file. If specified - trace will be sent to file instead of host.
[Parameter(ParameterSetName = 'File')]
[ValidateScript({
Test-Path $_ -IsValid
})]
[string]$FilePath
)
begin {
if ($FilePath) {
# assume we want to overwrite trace file
$PSBoundParameters.Force = $true
} else {
$PSBoundParameters.PSHost = $true
}
if ($Quiet) {
$Out = Get-Command Out-Null
$PSBoundParameters.Remove('Quiet') | Out-Null
} else {
$Out = Get-Command Out-Default
}
}
process {
Trace-Command @PSBoundParameters | & $Out
}
}
PS> New-Alias -Name tre -Value Trace-Expression
PS> Export-ModuleMember -Function * -Alias *
Check out this post for more details on playing detective with Trace-Command (@PowerShellMag).
When writing an object to the error stream, use the Write-Error cmdlet. This will allow you to manage any PowerShell errors in your scripts:
Write-Error "Access denied."
Check this post and learn how to format the Write-Error cmdlet.
Write-Debug writes a debug message to the console. When you write this in a function or script, it doesn’t do anything by default; the messages essentially lay in wait until you either modify your $DebugPreference or activate the -debug switch when calling a function or script. When $DebugPreference is set to ‘inquire’ or the -debug switch is activated, the message creates a breakpoint, giving you an easy way to pop into debug mode.
Take this example (@RJasonMorgan):
function Get-FilewithDebug {
[cmdletbinding()]
Param (
[parameter(Mandatory)]
[string]$path ) Write-Verbose “Starting script” Write-Debug “`$path is: $path” $return = Get-ChildItem -Path $path -Filter *.exe -Recurse -Force Write-Debug “`$return has $($return.count) items” $return }
The example above produces the following when run with -debug:
[C:\git] > Get-FilewithDebug -path C:\Users\jmorg_000\ -Debug
DEBUG: $path is: C:\Users\jmorg_000\
Confirm
Continue with this operation?
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):
What PowerShell commands do you use most often in your development work? Share your go-to cmdlets with us in the comments below. Another essential tool to add to your dev toolkit is Stackify Prefix, a dynamic code profiler that is simple, free, and efficient.
Featured Image Copyright: enotmaks / 123RF Stock Photo
If you would like to be a guest contributor to the Stackify blog please reach out to stackify@stackify.com