user3112318
user3112318

Reputation: 29

Cmd err “|” was unexpected at this time

i receive the message "Error: “|” was unexpected at this time batch script " when i run the code below:

@set CheckAccount=FOR /F "tokens=3" %A IN ('net user UserName /domain^|Findstr /ic: "active"') do SET Active=%A
%CheckAccount%
| was unexpected at this time.

Even when i change "active" to active. I can't seem to echo CheckAccount without getting that error message.

Upvotes: 0

Views: 928

Answers (1)

dxiv
dxiv

Reputation: 17648

There is an extra level of parsing because of the intermediate variable CheckAccount being used. This requires escaping the problem characters twice.

@set CheckAccount=FOR /F "tokens=3" %A IN ('net user UserName /domain^^^|Findstr /ic:"active"') do SET Active=%A

The set command will parse and store ^^^| as ^| into CheckAccount, then expanding %CheckAccount% will leave just the pipe |.


[ EDIT ] Removed the extra space after /ic:, thanks @Squashman for pointing that out.

Upvotes: 5

Related Questions