I'm using a WMIC command to output a list of SIDS and accompanying user profile names to text. From the text, I can edit a list of SIDS I need to add a set of registry keys to. However, the script that loops through the edited text file of SIDS is encoded in a format the script doesn't pick up and just fails to run. Using notepad++ I can re-encode from UCS-2 LE BOM to UTF-8 and then I can complete the script hassle free.
How can I make the output from the WMIC text default to UTF-8
?.
I noticed this on more that one PC. To cure the problem, as mentioned, I can re-encode in notepad++ but its a step I really need to avoid if possible. Trying to automate things as much as I can. The sole issue is the encoding, all other scripts, commands, codes etc. are fine once I get a UTF-8 text file. I use batch file often and like to output to text files, looking at those they are all defaulting to UTF-8 as expected. Seems specific to WMIC command here.
WMIC Path Win32_UserProfile Where "Special='False' And Not LocalPath='Null'" Get LocalPath,SID>somefile.txt
Gives all the info I need but outputs to UCS-2 LE BOM, not UTF-8
Any assistance would be great, thanks. (was thinking maybe a reg query would bypass the issue?)
The encoding of
wmic
's output depends on where the output is being sent/output
switch,wmic
will use UCS-2 LE BOMwmic
will use OEM codepageIf your scripts can not process the UCS-2 output one simple option (without third party tools) is to change where
wmic
writes by using a pipe.Here
wmic
's output is piped tofind /v ""
(find any non empty line) and then writen to a disk file using your OEM codepage.If you use this approach then you should note a curious side effect: the lines in the output don't end in a
CRLF
sequence, but aCRCRLF
sequence.If this is also a problem to your scripts then you can use the
type
command to read the output file and redirect its output to generate another one with ANSI encondingThe problem with this approach appears when the characters in the UCS-2 file don't have a direct equivalent in the ANSI codepage.
But if using a third party tool is a valid option, then aGerman's CONVERTCP tool (including source code if you prefer to compile it) is a good alternative to integrate in this kind of scripts.