Using a Variable (PowerShell) inside of a command

RogerLawrence picture RogerLawrence · Sep 12, 2012 · Viewed 20.1k times · Source
$computer = gc env:computername

# Argument /RU '$computer'\admin isn't working.
SchTasks /create /SC Daily /tn "Image Verification" /ST 18:00:00 /TR C:\bdr\ImageVerification\ImageVerification.exe /RU '$computer'\admin /RP password

Basically I need to provide the computer name in the scheduled task...

Thank you in advance!

Answer

Keith Hill picture Keith Hill · Sep 12, 2012

Single quoted strings will not expand variables in PowerShell. Try a double quoted string e.g.:

"$computer\admin"