1

I keep receiving random letters powershell script error. I made a changing mouse scheme script based on system os theme. I keep receiving random letters as shown in the image below when running the powershell script: Script Error

This is my script:

param (
    [Parameter(Mandatory=$true)][int]$daySegment2,  # 0 = Day, 1 = Night
    [Parameter(Mandatory=$true)][int]$daySegment4,  # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
    [Parameter(Mandatory=$true)][bool]$nightMode,   # True if night mode is enabled
    [Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)

$Location = "~\AppData\Local\WinDynamicDesktop\scripts\globalScripts\NightValue.xml"
$PreviousNightValue = Import-CliXml $Location

$Location2 = "~\AppData\Local\WinDynamicDesktop\scripts\globalScripts\StartValue.xml"
$StartValue = Import-CliXml $Location2

$NightValue = If ($nightMode) {1} Else {$daySegment2}

if ( ($NightValue -eq 0 -and $StartValue -eq 1) -or ($NightValue -ne $PreviousNightValue -and $StartValue -eq 0 -and $NightValue -eq 0))
{
$RegConnect = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]”CurrentUser”,”$env:COMPUTERNAME”)
$RegCursors = $RegConnect.OpenSubKey(“Control Panel\Cursors”,$true)
$RegCursors.SetValue(“”,”W11 Cursor Light HD v2.2 by Jepri Creations”)
$RegCursors.SetValue(“AppStarting”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\working.ani”)
$RegCursors.SetValue(“Arrow”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\pointer.cur”)
$RegCursors.SetValue(“Crosshair”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\precision.cur”)
$RegCursors.SetValue(“Hand”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\link.cur”)
$RegCursors.SetValue(“Help”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\help.cur”)
$RegCursors.SetValue(“IBeam”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\beam.cur”)
$RegCursors.SetValue(“No”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\unavailable.cur”)
$RegCursors.SetValue(“NWPen”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\handwriting.cur”)
$RegCursors.SetValue(“Person”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\person.cur”)
$RegCursors.SetValue(“Pin”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\pin.cur”)
$RegCursors.SetValue(“precisionhair”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\precision.cur”)
$RegCursors.SetValue(“SizeAll”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\move.cur”)
$RegCursors.SetValue(“SizeNESW”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\dgn2.cur”)
$RegCursors.SetValue(“SizeNS”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\vert.cur”)
$RegCursors.SetValue(“SizeNWSE”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\dgn1.cur”)
$RegCursors.SetValue(“SizeWE”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\horz.cur”)
$RegCursors.SetValue(“UpArrow”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\alternate.cur”)
$RegCursors.SetValue(“Wait”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\busy.ani”)
$RegCursors.Close()
$RegConnect.Close()
$CSharpSig = @’
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(
             uint uiAction,
             uint uiParam,
             uint pvParam,
             uint fWinIni);
‘@
$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo –PassThru
$CursorRefresh::SystemParametersInfo(0x0057,0,$null,0)
}
ElseIf (($NightValue -eq 1 -and $StartValue -eq 1) -or ($NightValue -ne $PreviousNightValue -and $StartValue -eq 0 -and $NightValue -eq 1))
{
$RegConnect = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]”CurrentUser”,”$env:COMPUTERNAME”)
$RegCursors = $RegConnect.OpenSubKey(“Control Panel\Cursors”,$true)
$RegCursors.SetValue(“”,”W11 Cursors Dark HD v2.2 by Jepri Creations”)
$RegCursors.SetValue(“AppStarting”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\working.ani”)
$RegCursors.SetValue(“Arrow”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\pointer.cur”)
$RegCursors.SetValue(“Crosshair”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\precision.cur”)
$RegCursors.SetValue(“Hand”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\link.cur”)
$RegCursors.SetValue(“Help”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\help.cur”)
$RegCursors.SetValue(“IBeam”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\beam.cur”)
$RegCursors.SetValue(“No”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\unavailable.cur”)
$RegCursors.SetValue(“NWPen”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\handwriting.cur”)
$RegCursors.SetValue(“Person”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\person.cur”)
$RegCursors.SetValue(“Pin”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\pin.cur”)
$RegCursors.SetValue(“precisionhair”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\precision.cur”)
$RegCursors.SetValue(“SizeAll”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\move.cur”)
$RegCursors.SetValue(“SizeNESW”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\dgn2.cur”)
$RegCursors.SetValue(“SizeNS”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\vert.cur”)
$RegCursors.SetValue(“SizeNWSE”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\dgn1.cur”)
$RegCursors.SetValue(“SizeWE”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\horz.cur”)
$RegCursors.SetValue(“UpArrow”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\alternate.cur”)
$RegCursors.SetValue(“Wait”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\busy.ani”)
$RegCursors.Close()
$RegConnect.Close()
$CSharpSig = @’
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(
                uint uiAction,
                uint uiParam,
                uint pvParam,
                uint fWinIni);
‘@
$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo –PassThru
$CursorRefresh::SystemParametersInfo(0x0057,0,$null,0)
}

$StartValue = 0
if (($NightValue -ne $PreviousNightValue))
{
    $NightValue | Export-Clixml -path $Location
}

if ($StartValue -ne 0)
{
    $StartValue | Export-Clixml -path $Location2
}

I tried removing some parts of the script, I think the problem stems for registry editing. But had no luck. the error kept showing no matter what I delete.

I tried putting the $PSStyle.OutputRendering='PlainText' at the top of my script, I received the same error. I tried setting the NO_COLOR to 1 inside my script. Did not fix the issue. I don't have access to the application that runs the script, I just can edit my script. BTW, that's started happening when the software started using PowerShell 7 instead of windows PowerShell(V5) I started getting this error.

Eyal Cohen
  • 121
  • 6
  • It looks to me like you have an *encoding* issue rather than a coding issue... :D What is the encoding of your `.ps1` file? – Keith Miller Feb 26 '23 at 18:20
  • utf 8 this is the error i'm receiving after running the script in utf 8 encoding: https://imgur.com/EOkjInu – Eyal Cohen Feb 26 '23 at 18:24

1 Answers1

3

Note: The following applies to PowerShell (Core) v7.2+:

What you're seeing are ANSI / VT (Virtual Terminal) escape sequences that are typically used to make terminals render text with formatting, notably colored.

If you render strings containing such escape sequences verbatim, such as via a message box, you'll get garbled text as in your screenshot, so you'll need to instruct PowerShell not to use these escape sequences.

Since it looks like you're calling your PowerShell script from the outside, via the pwsh.exe, the Windows PowerShell CLI, a simple way to instruct PowerShell not to use these escape sequences it to define the NO_COLOR environment variable (with any value, but 1 makes conceptual sense) before calling pwsh.exe.[1]

An alternative is to execute $PSStyle.OutputRendering='PlainText':

  • Either: as part of a -Command (-c) argument passed to pwsh.exe

  • Or: By placing this call at the top of your script.

For more information, see the conceptual about_ANSI_Terminals help topic.


Demonstration of the problem:

# Call the PowerShell (Core) 7.2+ CLI and provoke an error,
# then render the output via a message box:
(New-Object -ComObject WScript.Shell).Popup(
  (pwsh -c '1 / 0')
)

Result - note the garbled text:

screenshot with garbled text

The problem goes away if you define NO_COLOR first:

$env:NO_COLOR=1
(New-Object -ComObject WScript.Shell).Popup(
  (pwsh -c '1 / 0')
)

Result - no more garbled text:

screenshot - cleaned-up text


[1] If you define it for the calling process, the pwsh child process will inherit it. Unlike the $PSStyle.OutputRendering setting, NO_COLOR must already exist when the pwsh process is created in order to be effective. In cases where you're calling from a Unix shell, you can define the environmant variable in a command-scoped way, e.g. NO_COLOR=1 pwsh -c '1 / 0'

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I tried putting the $PSStyle.OutputRendering='PlainText' at the top of my script, I received the same error. I tried setting the NO_COLOR to 1 inside my script. Did not fix the issue. I don't have access to the application that runs the script, I just can edit my script. BTW, that's correct only when the software start using powershell 7 instead of windows powershell(V5) I started getting this error. – Eyal Cohen Feb 26 '23 at 19:36
  • 1
    @EyalCohen, the `NO_COLOR` environment variable must be set from _outside_ the PowerShell process, i.e. it must already exist when the `pwsh` process is created. Do you have a way to do that? – mklement0 Feb 26 '23 at 19:40
  • 1
    @EyalCohen, also, I just tried in v7.2.9 and v7.3.3: placing `$PSStyle.OutputRendering='PlainText'` at the top of a script works for me. – mklement0 Feb 26 '23 at 19:47
  • I am still having the same issue. I feel this is starting to become XY problem. check the image below: https://imgur.com/exsmrGZ Btw, when running the script in powershell ISE or from teminal no errors are thrown. Only when the 3rd party application runs the script. – Eyal Cohen Feb 26 '23 at 19:54
  • Adding the line $PSStyle.OutputRendering='PlainText' throws an error in the terminal/ISE: The property 'OutputRendering' cannot be found on this object. Verify that the property exists and can be set. At line:1 char:1 + $PSStyle.OutputRendering='PlainText' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound https://imgur.com/q9Op5bn – Eyal Cohen Feb 26 '23 at 19:59
  • 1
    @EyalCohen, save your `.ps1` file with UTF-8 encoding, which PowerShell (Core) now assumes by default. For PowerShell (Core), you don't need a BOM, but if you want Windows PowerShell to read the file correctly too, use _UTF-8 with BOM_. – mklement0 Feb 26 '23 at 20:01
  • 1
    @EyalCohen, you cannot run code that uses _PowerShell (Core)_-only features such as `$PSStyle` in the _ISE_, which supports _Windows PowerShell_ only. Please see the next comment for standard advice. – mklement0 Feb 26 '23 at 20:02
  • 1
    As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Feb 26 '23 at 20:02
  • 1
    I just moved to visual studio code to check my problems. Thanks for all of your suggestions you helped me alot. I never knew PowerShell ISE is limited to powershell 5. – Eyal Cohen Feb 26 '23 at 20:17
  • Glad to hear it, @EyalCohen; my pleasure. `$PSVersionTable` shows you what PowerShell version / edition you're running. – mklement0 Feb 26 '23 at 20:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252144/discussion-between-eyal-cohen-and-mklement0). – Eyal Cohen Feb 26 '23 at 20:36