Combining both PowerShell v5.1 and v6

Hey all,

With PS v6 Core about to be released, I was testing out some code written using the Invoke-WebRequest and Invoke-RestMethod CmdLets in v5.1 in comparison to v6. V5.1 was slow, and amazingly because of the underlying implementation changes, v6 is incredibly quick. A simple Invoke-WebRequest to GET a large file (4GB) yields a massive speed increase - I was seeing this file take nearly 1 hour to download from a server on the same subnet in v5.1, but in v6 this decreases to around 30 seconds.

This in itself was enough to say I would rather write only for v6 from now on. Unfortunately, other modules (such as PowerCLI) don’t work in v6, yet.

What would be the best way to go about building code that ran some stuff in v5.1 and other stuff in v6 (if it were available)?
Cheers

Chris

Same question was posted on another forum… See this post:
Combining both PowerShell v5.1 and v6 - PowerShell General - Ask the Experts - IDERA Community

A lot of effort has been put into the web cmdlets in v6.

In terms of combining them - the underlying issue is the version of .NET on which they’re built. V5.1 uses the full CLR and v6 uses .NET core (which is where the cross platform aspect comes from). It’s unlikely there’ll ever be 100% compatibility between v5.1 and v6.

Many of the v5.1 modules - especially those created as CDXML modules - will run under v6. The forthcoming Windows compatibility pack will enable more v5.1 modules to work with v6. Some functionality such as the AD cmdlets and the Exchange cmdlets won’t work in v6 even after the compatibility pack is available though you will be able to use the system.directoryservices classes and we may get the [ADSI] and [ADSISEARCHER] accelerators in v6.

if you’re planning on running v6 and v5.1 you’ll need to test your functionality to determine if it will work under both and in some cases accept that you need to stick with v5.1.

Hey Richard,

Indeed, the need might be to run BOTH v5 and v6 side by side, invoked from a single script - is that even possible?

For example, I would like to start the script in what ever is the default shell is, lets say v5. Then check to see if v6 is installed and available (e.g., checking to see if “C:\Program Files\PowerShell\6.0.0-rc.2\pwsh.exe” exists). Then to invoke some functions in one shell, whilst others run in the other.

Further, there are breaking changes to soem of the CmdLet in v6, such as the `Invoke-RestMethod` now required the authentication parameter for Basic authentication:

So, in v5, we had:

Invoke-RestMethod -Method Post -Uri $resource -cred $Creds -Body (ConvertTo-Json $data) -ContentType 'application/json

and in v6 we have:

Invoke-RestMethod -Method Post -Uri $resource -cred $Creds -Body (ConvertTo-Json $data) -ContentType 'application/json -Authentication Basic

I wondered if there was a simple way to a a full parameter + argument (-Authentication Basic) as a variable. I tried this simply as a variable string, but PS wouldn’t accept the `-Authentication` bit as a parameter). I think this need to be some kind of hash table).

I wondered if there was a simple way to a a full parameter + argument (-Authentication Basic) as a variable. I tried this simply as a variable string, but PS wouldn't accept the `-Authentication` bit as a parameter). I think this need to be some kind of hash table).

Do you refer to splatting?

@svilen, ah yes, that resolves passing the parameters.

I only wonder now about whether it is even possible to invoke one shell type from another.

This article shows one way of dealing with running v6 from v5.1