- 1 year ago
prnmngr.vbs - parse and format output as csv from a windows batch file?
The prnmngr.vbs
script is a built-in Windows script that allows you to manage printer-related tasks from the command line. If you want to parse and format its output as CSV in a Windows batch file, you can use a combination of batch commands and PowerShell. Here's how you can do it:
Run prnmngr.vbs and Capture Output:
In your batch file, you can use thecscript
command to run theprnmngr.vbs
script and capture its output. For example:batch@echo off
setlocal
REM Run prnmngr.vbs and capture output to a temporary file
cscript //nologo "%systemroot%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs" -l > temp.txt
REM Process the captured output using PowerShell
powershell -ExecutionPolicy Bypass -Command ^
"Get-Content 'temp.txt' | ForEach-Object {"^
" $line = $_ -replace '\s+', ','"^
" Write-Host $line"^
"}" > output.csv
REM Clean up the temporary file
del temp.txt
endlocal
Explanation:
- The batch script uses
cscript
to runprnmngr.vbs
and redirect its output to a temporary text file (temp.txt
). - It then uses PowerShell to process the text file and replace spaces with commas to format the output as CSV.
- The resulting CSV output is saved to an
output.csv
file. - Finally, the temporary text file is deleted.
- The batch script uses
Run the Batch File:
Save the batch script to a.bat
file and run it from the command prompt.
Keep in mind that this approach assumes that the prnmngr.vbs
output has a predictable structure. If the output structure changes, you might need to adjust the PowerShell part to properly parse and format the data.
Also, note that the provided script captures the printer list (-l
option) from prnmngr.vbs
as an example. Depending on the operation you're performing with prnmngr.vbs
, you'll need to adjust the command line options accordingly.
Lastly, ensure that your Windows environment has the required permissions to run batch scripts and execute PowerShell commands.