Scripting with Powershell 7

dfletcher
dfletcher Member Posts: 28 ✭✭
edited September 2023 in Scripts

We script against some 3rd party powershell modules that require Powershell 7. Running these scripts through Atera fails because it defaults to powershell.exe (Powershell 5.1) when powershell 7 requires pwsh.exe.

I found a workaround here that is working for us:

You can definitely do this it's just not builtin using a pwsh syntax. The easiest way I know is to just start the new process and use a scriptblock as the argument.

pwsh.exe {
  code inside here runs in the new process
  }

PowerShell internally will convert that scriptblock into -EncodedCommand ... and output the data as actual objects so you can get richer objects back to use in the main process. They will be serialized so essentially a read only copy of what was output.

Tagged:

Comments