Search code examples
powershellautomationversionpower-automatepower-automate-desktop

Can I change powershell version to 7 with "Run PowerShell script" action on Power Automate Desktop?


Can I change powershell version to 7 on "Run PowerShell script" action? If I can, how?

I know this post but I couldn't find if I can change the version PAD execute. https://powerusers.microsoft.com/t5/Power-Automate-Desktop/Powershell-and-other-script-SUPPORTED-VERSION/td-p/1501322

I already installed powershell version7. I'd like to use "-UseQuotes" option on "Export-Csv".

FYI, my PSVersion is here. PSVersion 5.1.19041.1320

Thank you,

I also checked registry about PAD but There is noregistry to manage powershell


Solution

  • This is a bit ugly but it will get the job done.

    I have this very basic flow, it just runs the PS script and outputs the results in a message box.

    Flow

    Throw this into the PowerShell task and then view the output.

    $psVersion = (Get-Host).Version.Major
    
    if ($psVersion -ne 7) {
        $processInfo = New-Object System.Diagnostics.ProcessStartInfo
    
        $processInfo.FileName = "C:\Program Files\PowerShell\7\pwsh.exe"
        $processInfo.RedirectStandardError = $true
        $processInfo.RedirectStandardOutput = $true
        $processInfo.UseShellExecute = $false
        $processInfo.Arguments = $MyInvocation.MyCommand.Definition
    
        $process = New-Object System.Diagnostics.Process
        $process.StartInfo = $processInfo
        $process.Start() | Out-Null
        $process.WaitForExit()
    
        $stdout = $process.StandardOutput.ReadToEnd()
        $stderr = $process.StandardError.ReadToEnd()
    
        Write-Host $stdout
    } else {
        # Your logic goes here!
        Write-Host "PowerShell Version = $psVersion"  
    }
    

    You should get this ...

    Result

    Essentially, it's checking for the version and if it's not version 7, it will launch PowerShell 7 and then retrieve the output.

    Take note of the file path for PS7, you may need to change that or you may be able to simplify it. I've gone with the full path to make sure it works.