header image

Archive for PowerShell 7

Test-Connection

Posted by: | October 28, 2019 Comments Off on 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.

under: PowerShell 7

Null coalescing with Object properties

Posted by: | October 28, 2019 Comments Off on 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.

under: PowerShell 7

Null coalescing operators

Posted by: | October 25, 2019 Comments Off on 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

under: PowerShell 7

PowerShell v7 preview 5 experimental features

Posted by: | October 24, 2019 Comments Off on 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.

under: PowerShell 7

PowerShell v7 preview 5

Posted by: | October 24, 2019 Comments Off on 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

under: PowerShell 7

Ternary operator

Posted by: | September 25, 2019 Comments Off on Ternary operator |

PowerShell v7 preview 4 adds a Ternary operator to PowerShell.

 

A ternary operator is a way to provide shortened coding for a simple if-else block. Its an operator that takes three operands rather than the usual two hence the name ternary.

for example

PS> $a = 5
PS> $b = 3
PS> if ($a -gt $b){‘Greater’}else{‘Not greater’}
Greater

 

Using a ternary operator this becomes

PS> $a -gt $b ? ‘Greater’ : ‘Not greater’
Greater

 

The generic form is

<condition> ? <value if true> : <value if false>

 

The ternary operator is an experimental feature so needs to be enabled

PS> Enable-ExperimentalFeature PSTernaryOperator

 

and PowerShell restarted.

 

If I use the new operator I think its going to be when I’m working interactively. For scripts and modules I prefer to use the more verbose if statement as its easier to read and understand.

under: PowerShell 7

Foreach-Object -parallel

Posted by: | August 30, 2019 Comments Off on Foreach-Object -parallel |

The introduction of Foreach-Object -parallel in PowerShell v7 preview 3 brings some much needed parallelisation options back into PowerShell.

 

PowerShell workflows are available in Windows PowerShell but are quirky (to be kind) and can be difficult to use. Workflows were removed in PowerShell v6.0

 

In PowerShell v7 preview 3 Foreach-Object receives a –parallel parameter that takes a scriptblock as its value.

 

As a simple example consider this simple counting example from the PowerShell team blog:

PS> (Measure-Command -Expression {1..50 | ForEach-Object {Start-Sleep -Milliseconds 100}}).TotalSeconds
5.4960846

 

Now using the parallel option

PS> (Measure-Command -Expression {1..50 | ForEach-Object -Parallel {Start-Sleep -Milliseconds 100}}).TotalSeconds
2.2523929

 

The time to execute is significantly reduced.

 

A few caveats are needed.

Firstly, the parallel option is an experimental feature so must be enabled

Enable-ExperimentalFeature -Name PSForEachObjectParallel

 

Secondly, not all tasks are suitable candidates for parallel execution. Expect more discussion on this topic in the near future including contributions on the PowerShell team blog.

under: PowerShell 7

Get-AdUser in PowerShell Core

Posted by: | August 29, 2019 Comments Off on Get-AdUser in PowerShell Core |

There has been a problem with Get-ADUser in PowerShell core such that

Get-ADUser -Identity Richard -Properties *

 

Throws an error.

 

The problem is in .NET Core and affects a small number of properties including ProtectedFromAccidentalDeletion

 

The underlying .NET Core issue has been fixed and PowerShell v7 preview 3 on Windows 10.0.18362 will successfully run the command

under: PowerShell 7, PowerShell and Active Directory, PowerShell v6

Experimental features

Posted by: | August 23, 2019 Comments Off on Experimental features |

PowerShell core has recently. v6.2, had the concept of experimental features added. An experimental feature is new or changed functionality that may be a breaking change or about which the PowerShell team want feedback before finalising the code.

My PowerShell v6.2.2 instance has the following experimental features

PSImplicitRemotingBatching
PSUseAbbreviationExpansion
PSCommandNotFoundSuggestion
PSTempDrive

 

of which I’ve enabled PSCommandNotFoundSuggestion and PSTempDrive. Experimental features are disbaled by default in PowerShell v6.2

 

My PowerShell v7 preview 3 instance has the following experimental features

PSImplicitRemotingBatching
PSForEachObjectParallel
PSCommandNotFoundSuggestion

 

of which PSForEachObjectParallel and PSCommandNotFoundSuggestion

PSUseAbbreviationExpansion and PSTempDrive and now full features in PowerShell v7

 

If don’t have any PowerShell v6.2 experimental features enabled OR don’t have v6.2 installed and then install PowerShell v7 preview 3, or later, all experimental features will be enabled by default. If you’ve enabled any experimental features in PowerShell v6.2 then PowerShell v7 respects the settings and doesn’t enable all experimental features.

 

You can view the settings controlling experimental features (among other things) at

Get-Content –Path $home\Documents\PowerShell\powershell.config.json

 

The single settings file is used for PowerShell v6.2 and PowerShell v7 previews

under: PowerShell 7, PowerShell v6

PowerShell v7 preview 3

Posted by: | August 21, 2019 Comments Off on PowerShell v7 preview 3 |

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

 

Breaking changes seem to be confined to non-Windows platforms with the removal of the kill alias on Stop-Process and support for pwsh as a login shell

 

The big new item is the –parallel parameter on Foreach-Object – more on this later.

 

In this preview version and future preview versions all experimental features will be enabled going forward

under: PowerShell 7

« Newer Posts - Older Posts »

Categories