We help IT Professionals succeed at work.
Get Started

Powershell - concatenated command will not run

garryshape asked
Last Modified: 2014-11-20
I am trying to string together a Schtasks command (for task scheduler) in powershell.

Through multiple variables, I successfully create the following:

$ScheduleAdd = "Schtasks /create /tn " + $userAlias +"-Covering-" + $identityAlias + " /sc ONCE /SD " + $single_StartDate + " /st 00:01 " + "/tr " + $([char]34) + "Powershell -command " + $addScript + $([char]34)
	Write-Host $ScheduleAdd

Open in new window

if I run "Write-Host $ScheduleAdd" after it in the script, it will successfully write to the console the exact command that can be copied and pasted into command prompt and run to create the scheduled task.

So I'm trying to figure out, why is it not actually adding the task to task scheduler when I set the variable above?

I can copy/paste it into the prompt and run it, and it adds, but if I don't copy paste the command, it will not add?

So Write-Host $ScheduleAdd will print out just like this:

Schtasks /create /tn TSmith-Covering-BJones /sc ONCE /SD 11-24-2014 /st 00:01 /tr "Powershell -command \\server\t$\schedules\Add-TSmith-Covering-BJones-11-24-2014.ps1"

It will print that out, but Powershell isn't executing it. I have to copy and paste it into the powershell window, and then it executes it, adding it to the scheduled tasks.
Watch Question
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE