Test-Connection

In PowerShell v7 preview Test-Connection sees some improvements:

PS> Test-Connection -TargetName 127.0.0.1 | Format-List

Source      : W510W10
Destination : 127.0.0.1
Replies     : {System.Net.NetworkInformation.PingReply, System.Net.NetworkInformation.PingReply,
               System.Net.NetworkInformation.PingReply, System.Net.NetworkInformation.PingReply}

The progress bar has been removed as has the unrequired text displayed with each ping.

The replies are still wrapped up in a collection of objects each of which looks like this

PS> Test-Connection -TargetName 127.0.0.1 -Count 1 | select -ExpandProperty Replies

Status        : Success
Address       : 127.0.0.1
RoundtripTime : 0
Options       : System.Net.NetworkInformation.PingOptions
Buffer        : {97, 98, 99, 100…}

Using the Quiet parameter

PS> Test-Connection -TargetName 127.0.0.1 -Quiet
True

Works exactly as expected

TargetName is called ComputerName in Windows PowerShell versions of the cmdlet though the PowerShell v7 versions supplies ComputerName as an alias.

The preview 5 version of Test-Connection is starting to become usable though to be considered truly fit for purpose the Replies objects need to be unravelled and the output should consist of one object per ping NOT just a single object.

Posted in PowerShell 7 | Leave a comment

Null coalescing with Object properties

The Null coalescing operators were introduced in PowerShell v7 preview 5. last time you saw how to use Null coalescing with variables. You can also use Null coalescing with Object properties.

Let’s first create an object.

PS> $prop = @{Nullprop = $null; NonNullProp = ‘something’}
PS> $obj = New-Object -TypeName PSobject -Property $prop

Yes, I know there other ways but I prefer this approach.

Using the Null coalescing operator

PS> $obj.NonNullProp ?? ‘is null’
something
PS> $obj.NullProp ?? ‘is null’
is null

is very similar to working with variables.

The Null coalescing assignment operator

PS> $obj.NonNullProp ??= ‘is null’
PS> $obj.NullProp ??= ‘is null’
PS> $obj | Format-List

NonNullProp : something
Nullprop    : is null

If the property is non-null nothing happens. If the property is null the property is assigned the value on the right hand side of the operator.

To my mind the Null coalescing operator and the Ternary operator introduced in PowerShell v7 preview 4 fall into the same category of great if you need to perform these operations on a regular basis and are used to thinking in these terms but otherwise not something to bother with. We’ve managed to survive through 6 versions of PowerShell without them and I don’t see them as a great step forward or particularly useful for IT administrators.

Posted in PowerShell 7 | Leave a comment

Null coalescing operators

Powershell v7 preview 5 introduces a new experimental feature that supports Null coalescing operators and null coalescing assignment operstors.

As its an experimental feature:

PS> Get-ExperimentalFeature -Name PSCoalescingOperators | Format-List

Name        : PSCoalescingOperators
Enabled     : False
Source      : PSEngine
Description : Support the null coalescing operator and null coalescing assignment operator in PowerShell language

You have to enable it:

PS> Enable-ExperimentalFeature -Name PSCoalescingOperators
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.

And then restart PowerShell.

Create a variable with $null as its value

PS> $nvar = $null

Using the coalescing operator
PS> $nvar ?? ‘is null’
is null

if $nvar is null the string is returned

By contrast a non-null value won’t return the ‘is null’ string

PS> $num = 1
PS> $num ?? ‘is null’
1
PS> $str = ‘aaa’
PS> $str ?? ‘is null’
aaa

The ?? operator checks the left hand side to be null or undefined and returns the value of right hand side if null, else return the value of left hand side.

The coalescing operator is equivalent to

PS> if ($nvar -eq $null) {‘is null’}else {$nvar}
is null

The  Null assignment operator  ??=  operator check the left hand side to be null or undefined and assigns the value of right hand side if null, else the value of left hand side is not changed.

PS> $date1 = Get-Date

PS> $date2 ??= Get-Date
PS> $date1

25 October 2019 21:19:48

PS> $date2

25 October 2019 21:22:03

Using the null coalescing assignment operator is equivalent to

PS> if ($date2 -eq $null) {$date2 = Get-Date}
PS> $date2

25 October 2019 21:24:09

Posted in PowerShell 7 | Leave a comment

PowerShell v7 preview 5 experimental features

PowerShell v7 preview 5 has a number of new, and some modified experimental features. The full list of PowerShell v7 preview 5 experimental features is:

PSCoalescingOperators  – –  Support the null coalescing operator and null coalescing
assignment operator in PowerShell language

PSCommandNotFoundSuggestion  – –  Recommend potential commands based on fuzzy search on a  CommandNotFoundException

PSErrorView  – –  New formatting for ErrorRecord

PSForEachObjectParallel   – –    New parameter set for ForEach-Object to run script blocks
   in parallel

PSImplicitRemotingBatching  – –  Batch implicit remoting proxy commands to improve
performance

PSPipelineChainOperators  – –  Allow use of && and || as operators between pipeline
invocations

PSTernaryOperator  – –  Support the ternary operator in PowerShell language

PSUpdatesNotification  – –  Print notification message when new releases are available

Microsoft.PowerShell.Utility.PSGetError  – –  Enable Get-Error cmdlet that displays detailed information about ErrorRecords included nested objects

Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace   – –  Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger.

PSDesiredStateConfiguration.InvokeDscResource  – –  Enables the Invoke-DscResource cmdlet and related features.

Its an interesting set of features that I’ll work through in subsequent posts.

Posted in PowerShell 7 | Leave a comment

PowerShell v7 preview 5

PowerShell v7 preview 5 is now available from https://github.com/PowerShell/PowerShell/releases

There are a number of new features – released as experimental features for the most part – and some bug fixes.

Test-Connection has lost the unnecessary output but still doesn’t format the output properly

Posted in PowerShell 7 | Leave a comment

Hidden files

If you suspect there are hidden files in a folder you can use the Attributes parameter to discover them:

PS> Get-ChildItem -Path c:\test -Attributes H

OR

PS> Get-ChildItem -Path c:\test -Attributes h

OR

PS> Get-ChildItem -Path c:\test -Hidden

If you want to see all files irrespective of them being hidden use Force

PS> Get-ChildItem -Path c:\test –Force

which will also show system files.

There are a number of ways to make a file hidden but the most generic is:

PS> Get-ChildItem -Path C:\test\Newoutdata03.txt  | ForEach-Object {$_.Attributes += ‘Hidden’}

You can modify the criteria used to define the files passed into Foreach-Object by using the path,  filter, exclude or include parameters of Get-ChildItem. The Hidden attribute is set while preserving the other attributes.

To remove the Hidden attribute:

PS> Get-ChildItem -Path C:\test\ -Hidden | ForEach-Object {$_.Attributes -= ‘Hidden’}

The Hidden attribute is removed whilst preserving other attributes. The files passed into ForEach-Object can again be filtered with the Get-ChildItem parameters.

Posted in Powershell | Leave a comment

Receive-Job Keep parameter

The Receive-Job Keep parameter is required if you want the data contained in the job to remain available. if you don’t use the –Keep parameter (a switch) the data will be deleted.

Its a pain to remember to use the Keep parameter. I’ve been working with jobs a lot just recently and the number of times I had to rerun jobs because I forgot to use Keep doesn’t bear thinking about.

I finally got round to adding it to the default parameter values.

This line in my profile

$PSDefaultParameterValues = @{‘Install-Module:Scope’=’AllUsers’; ‘Update-Module:Scope’=’AllUsers’; ‘Receive-Job:Keep’ = $true}

ensures I won’t forget ever again.  Because Keep is a switch you have to set the value to true.

Posted in Powershell | Leave a comment