Using a Stop Watch

In PowerShell, to measure time, you can simply subtract datetime values from another:

$Start = Get-Date

$null = Read-Host -Prompt "Press ENTER as fast as you can!"

$Stop = Get-Date
$TimeSpan = $Stop - $Start

An elegant alternative is a stop watch:

$StopWatch = [Diagnostics.Stopwatch]::StartNew()

$null = Read-Host -Prompt "Press ENTER as fast as you can!"


The specific stop watch advantage is its ability to be paused and continued.

Twitter This Tip! ReTweet this Tip!

Using Solid Alternatives for $MyInvocation

Lines like $MyInvocation.MyCommand.Definition can be useful to determine the folder in which the current script is stored, i.e. to access other resources located in the same folder.

However, ever since PowerShell 3, there have been easy alternatives to find out the current script name and/or the path to the folder that contains the current script. Run the following code to test for yourself:


Split-Path -Path $MyInvocation.MyCommand.Definition

If you run these lines interactively (or in an “Untitled” script), they all return nothing. Once you save the script and run the script file, though, the first two lines return the script file path, and the second two lines return the folder path in which the script is located.

The good thing about $PSCommandPath and $PSScriptRoot is that they always contain the same information. $MyInvocation, in contrast, can change, and will change once this variable is read from within a function:

function test

    Split-Path -Path $MyInvocation.MyCommand.Definition


Now, $MyInvocation turns out to be useless because it always returns information about who invoked the current script block.

Twitter This Tip! ReTweet this Tip!

Finding Open Firewall Ports

Here is a piece of PowerShell code that connects to the local firewall and dumps the open firewall ports:

$firewall = New-object -ComObject HNetCfg.FwPolicy2
$firewall.Rules |  Where-Object {$_.Action -eq 0} | 
    Select-Object Name, ApplicationName,LocalPorts

The result may look similar to this:

Name           ApplicationName                                         LocalPorts
----           ---------------                                         ----------
pluginhost.exe C:userstobweappdatalocalskypepluginpluginhost.exe *         
pluginhost.exe C:userstobweappdatalocalskypepluginpluginhost.exe *         
spotify.exe    C:userstobweappdataroamingspotifyspotify.exe      *         
spotify.exe    C:userstobweappdataroamingspotifyspotify.exe      *     

In Windows 10 and Server 2016, there are finally a number of firewall-specific cmdlets out of the box, too:

PS> Get-Command -Noun *Firewall*

CommandType     Name                                               Version    Source                    
-----------     ----                                               -------    ------                    
Function        Copy-NetFirewallRule                         NetSecurity               
Function        Disable-NetFirewallRule                      NetSecurity               
Function        Enable-NetFirewallRule                       NetSecurity               
Function        Get-NetFirewallAddressFilter                 NetSecurity               
Function        Get-NetFirewallApplicationFilter             NetSecurity               
Function        Get-NetFirewallInterfaceFilter               NetSecurity               
Function        Get-NetFirewallInterfaceTypeFilter           NetSecurity               
Function        Get-NetFirewallPortFilter                    NetSecurity               
Function        Get-NetFirewallProfile                       NetSecurity               
Function        Get-NetFirewallRule                          NetSecurity               
Function        Get-NetFirewallSecurityFilter                NetSecurity               
Function        Get-NetFirewallServiceFilter                 NetSecurity               
Function        Get-NetFirewallSetting                       NetSecurity               
Function        New-NetFirewallRule                          NetSecurity               
Function        Remove-NetFirewallRule                       NetSecurity               
Function        Rename-NetFirewallRule                       NetSecurity               
Function        Set-NetFirewallAddressFilter                 NetSecurity               
Function        Set-NetFirewallApplicationFilter             NetSecurity               
Function        Set-NetFirewallInterfaceFilter               NetSecurity               
Function        Set-NetFirewallInterfaceTypeFilter           NetSecurity               
Function        Set-NetFirewallPortFilter                    NetSecurity               
Function        Set-NetFirewallProfile                       NetSecurity               
Function        Set-NetFirewallRule                          NetSecurity               
Function        Set-NetFirewallSecurityFilter                NetSecurity               
Function        Set-NetFirewallServiceFilter                 NetSecurity               
Function        Set-NetFirewallSetting                       NetSecurity               
Function        Show-NetFirewallRule                         NetSecurity   

Twitter This Tip! ReTweet this Tip!