In PowerShell Diskpart Script Does Not Work (Despite No Error) Via “diskpart /s nameofscript.txt”

Goal:  To use a diskpart.exe script (a file of commands that could be ran interactively in sequence).
Problem:  When you run this command via PowerShell it does not work:  diskpart /s C:\Full\Path\script.txt
The result says something like "Microsoft DiskPart ..." and there is a list of alphabetic diskpart subcommands (ACTIVE to RECOVER).  There is no explicit error.  The diskpart command and script does no work.
Background:  When a PowerShell redirect (>) command is used to save a file, the default encoding is usually Unicode.  This encoding will not work for a diskpart script file (e.g., a .txt file of commands that could run interactively in sequence once diskpart is entered). 
Solution:  To resolve this, open the script, go to Save As, change the "Encoding" from "Unicode" to "ANSI."  Alternatively, if the diskpart script you are using is generated via PowerShell on an ongoing basis, use the Out-File and Encoding flags to designate the file to be ASCII.

Leave a comment

Your email address will not be published. Required fields are marked *