2

I've been trying to use the following code to quickly display a computer's serial number through command prompt. Trying to use the powershell command because Microsoft will be getting rid of WMIC.

FOR /F "usebackq delims=" %%A IN ('powershell.exe -NoProfile -Command "Get-CimInstance Win32_BIOS | Select-Object -ExpandProperty SerialNumber"') DO (SET "serial=%%A")

ECHO %serial%

When run from the batch file the serial number that gets displayed to the screen is:

powershell.exe -NoProfile -Command "Get-CimInstance Win32_BIOS | Select-Object -ExpandProperty SerialNumber"

How can I make this work?

New contributor
Jerry Losorata is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

1

Just get rid of the quotes in SET:

FOR /F "usebackq delims=" %%A IN (`powershell.exe -NoProfile -Command "Get-CimInstance Win32_BIOS | Select-Object -ExpandProperty SerialNumber"`) DO SET serial=%%A

In plain Powershell it would be much simpler:

 Get-CimInstance Win32_BIOS | Select-Object -ExpandProperty SerialNumber | Set-Item -Path ENV:serial
0
1

Got it to work with the following:

SET "serial="

FOR /F "usebackq delims=" %%A IN (`powershell.exe -NoProfile -Command "Get-CimInstance Win32_BIOS | Select-Object -ExpandProperty SerialNumber"`) DO (SET serial=%%A)

ECHO %serial%
New contributor
Jerry Losorata is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Hi Jerry, you are able to tick your answer 24hrs after the question was posted Commented 7 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.