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
$TimeSpan.TotalMilliseconds

An elegant alternative is a stop watch:

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

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

$StopWatch.Stop()
$StopWatch.ElapsedMilliseconds

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:

$MyInvocation.MyCommand.Definition
$PSCommandPath

Split-Path -Path $MyInvocation.MyCommand.Definition
$PSScriptRoot

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
{
    $MyInvocation.MyCommand.Definition
    $PSCommandPath

    Split-Path -Path $MyInvocation.MyCommand.Definition
    $PSScriptRoot
}

test 

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                               2.0.0.0    NetSecurity               
Function        Disable-NetFirewallRule                            2.0.0.0    NetSecurity               
Function        Enable-NetFirewallRule                             2.0.0.0    NetSecurity               
Function        Get-NetFirewallAddressFilter                       2.0.0.0    NetSecurity               
Function        Get-NetFirewallApplicationFilter                   2.0.0.0    NetSecurity               
Function        Get-NetFirewallInterfaceFilter                     2.0.0.0    NetSecurity               
Function        Get-NetFirewallInterfaceTypeFilter                 2.0.0.0    NetSecurity               
Function        Get-NetFirewallPortFilter                          2.0.0.0    NetSecurity               
Function        Get-NetFirewallProfile                             2.0.0.0    NetSecurity               
Function        Get-NetFirewallRule                                2.0.0.0    NetSecurity               
Function        Get-NetFirewallSecurityFilter                      2.0.0.0    NetSecurity               
Function        Get-NetFirewallServiceFilter                       2.0.0.0    NetSecurity               
Function        Get-NetFirewallSetting                             2.0.0.0    NetSecurity               
Function        New-NetFirewallRule                                2.0.0.0    NetSecurity               
Function        Remove-NetFirewallRule                             2.0.0.0    NetSecurity               
Function        Rename-NetFirewallRule                             2.0.0.0    NetSecurity               
Function        Set-NetFirewallAddressFilter                       2.0.0.0    NetSecurity               
Function        Set-NetFirewallApplicationFilter                   2.0.0.0    NetSecurity               
Function        Set-NetFirewallInterfaceFilter                     2.0.0.0    NetSecurity               
Function        Set-NetFirewallInterfaceTypeFilter                 2.0.0.0    NetSecurity               
Function        Set-NetFirewallPortFilter                          2.0.0.0    NetSecurity               
Function        Set-NetFirewallProfile                             2.0.0.0    NetSecurity               
Function        Set-NetFirewallRule                                2.0.0.0    NetSecurity               
Function        Set-NetFirewallSecurityFilter                      2.0.0.0    NetSecurity               
Function        Set-NetFirewallServiceFilter                       2.0.0.0    NetSecurity               
Function        Set-NetFirewallSetting                             2.0.0.0    NetSecurity               
Function        Show-NetFirewallRule                               2.0.0.0    NetSecurity   
 

Twitter This Tip! ReTweet this Tip!