Skip to content

Fix: PowerShell commands fail with custom profile scripts#403

Open
AmonReviled wants to merge 2 commits intohass-agent:mainfrom
AmonReviled:fix-powershell-noprofile
Open

Fix: PowerShell commands fail with custom profile scripts#403
AmonReviled wants to merge 2 commits intohass-agent:mainfrom
AmonReviled:fix-powershell-noprofile

Conversation

@AmonReviled
Copy link

@AmonReviled AmonReviled commented Jan 3, 2026

This PR fixes issue #264 where PowerShell sensors and commands fail when users have custom profile scripts (e.g., Starship terminal prompt).

Problem

When executing PowerShell commands or scripts, HASS.Agent was not using the -NoProfile flag. This caused custom profile scripts (like Microsoft.PowerShell_profile.ps1) to be loaded, which could:

  • Append error messages to the sensor output
  • Cause "Cannot run script on machine" errors
  • Include unwanted output from profile initialization
image

Solution

Added -NoProfile flag to all PowerShell executions in PowershellManager.cs:

  1. GetProcessArguments method - Used by ExecuteHeadless and Execute:

    • Script: -NoProfile -File "{command}"
    • Command: -NoProfile -EncodedCommand {encodedCommand}
  2. ExecuteWithOutput method - Used by sensors for testing:

    • Script: -NoProfile & '{command}'
    • Command: -NoProfile & {{{command}}}

The -NoProfile flag tells PowerShell to skip loading any profile scripts, ensuring clean and predictable execution.

Closes #264

@AmonReviled AmonReviled force-pushed the fix-powershell-noprofile branch from 2e6a04d to c097d96 Compare January 3, 2026 13:31
@lukasz-lech-enova-expert

Hello
If I may, I would suggest one more change:

in the method ExecuteWithOutput instead of:

                Arguments = isScript
                    ? $@"-NoProfile & '{command}'"
                    : $@"-NoProfile & {{{command}}}"

put:

                Arguments = GetProcessArguments(command,"",isScript)

This will make executing powershell more consistant and fix another issue.
In old style Powershell was called like this (let's take "$PSVersionTable.PSVersion | Out-String" as an example)

"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& {$PSVersionTable.PSVersion | Out-String}"

For version 5.1 it works fine but for 7+ it is not:

"C:\Program Files\PowerShell\7\pwsh.exe" "& {$PSVersionTable.PSVersion | Out-String}"

gives an error.

Using GetProcessArguments fixes this.

Best Regards
Luke

@AmonReviled
Copy link
Author

Hello If I may, I would suggest one more change:

in the method ExecuteWithOutput instead of:

                Arguments = isScript
                    ? $@"-NoProfile & '{command}'"
                    : $@"-NoProfile & {{{command}}}"

put:

                Arguments = GetProcessArguments(command,"",isScript)

This will make executing powershell more consistant and fix another issue. In old style Powershell was called like this (let's take "$PSVersionTable.PSVersion | Out-String" as an example)

"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& {$PSVersionTable.PSVersion | Out-String}"

For version 5.1 it works fine but for 7+ it is not:

"C:\Program Files\PowerShell\7\pwsh.exe" "& {$PSVersionTable.PSVersion | Out-String}"

gives an error.

Using GetProcessArguments fixes this.

Best Regards Luke

@lukasz-lech-enova-expert done, thank you!

@amadeo-alex amadeo-alex added the PR - In progress PR review was started and ongoing label Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR - In progress PR review was started and ongoing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: "Cannot run script on machine" error when trying to run Powershrll scripts with a custom profile script.

4 participants