RSS

PowerShell – PowerShell generated batch file results in error þ is not recognized as an internal or external command

31 Mar
PowerShell – PowerShell generated batch file results in error þ is not recognized as an internal or external command

Some time ago, a colleague told me he was having trouble generating a batch file using PowerShell. The batch file as generated, but when he tried to run the batch file, it resulted in an error containing:
þ is not recognized as an internal or external command

In the batch file, the character þ was nowhere to be found.

The code he used to generate the batch file was as follows:

$Batscript = ‘D:\batscript.cmd’

IF(Test-Path -path $Batscript)

{

Remove-Item $Batscript

}

$SSIDImport = Get-Content ‘D:\SSID.txt’

Foreach($SSID in $SSIDImport)

{

“nsrmm.exe -d -S $SSID -y $SSID | Out-File -filepath $Batscript -Force -Append

}

‘nsrim.exe -X’ | Out-File -FilePath $Batscript –Append

After Googling a bit I stumbled across a similar issue: http://powershell.org/wp/forums/topic/strange-character-error-when-running-native-cmdline-app/

Thanks to Dave Wyatt I learned that the default encoding for Out-Filepath is Unicode which many command line tools cannot handle. With this knowledge, the solution was to simply specify the encoding for Out-File as shown below.

$Batscript = ‘D:\batscript.cmd’

IF(Test-Path -path $Batscript)

{

Remove-Item $Batscript

}

$SSIDImport = Get-Content ‘D:\SSID.txt’

Foreach($SSID in $SSIDImport)

{

“nsrmm.exe -d -S $SSID -y $SSID | Out-File -filepath $Batscript -Force -Append -Encoding ascii

}

‘nsrim.exe -X’ | Out-File -FilePath $Batscript –Append -encoding ascii

After this change, everything worked perfectly.

 

 
1 Comment

Posted by on March 31, 2016 in ICT, Microsoft, Powershell

 

Tags: , , , , , , , , ,

One response to “PowerShell – PowerShell generated batch file results in error þ is not recognized as an internal or external command

  1. Andy

    May 15, 2017 at 19:19

    Over a year later, and this was a huge help. Thanks.

    Like

     

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.