Page 1 of 1

Bat script to shutdown computers and txt file

Posted: 03 Aug 2023 00:11
by sgboi82
Hi, I would need help from the expert on writing a bat file to shutdown computers based on the text file (PCNAME).

The script would read the txt file and then loop to shutdown all the pc based on the pc name in the txt file.

Re: Bat script to shutdown computers and txt file

Posted: 03 Aug 2023 04:56
by Batcher
test-1.bat

Code: Select all

@echo off
cd /d "%~dp0"
for /f "delims=" %%i in ('type "PCNAME.txt"') do (
    shutdown /s /f /t 0 /m "%%i"
)

Re: Bat script to shutdown computers and txt file

Posted: 03 Aug 2023 15:05
by ShadowThief
Why are you parsing the output of type? You can just interact with the file directly in the for loop.

Code: Select all

for /f "usebackq delims=" %%A in ("PCNAME.txt") do shutdown /s /f /t 0 /m \\%%A
(note that I'm assuming that the PC names don't have the \\ prefix in the text file that they need to be passed to the shutdown command.)

Re: Bat script to shutdown computers and txt file

Posted: 03 Aug 2023 19:42
by Batcher
type "PCNAME.txt" works if txt is Unicode or ANSI.