Search code examples
powershelldouble-quotesquotingsingle-quotes

I have problem with putting quotes inside quotes


I am trying to make one line command in PowerShell to download music file and to execute it every 1 hour
Start-Process -WindowStyle Hidden PowerShell "Register-ScheduledTask Regedit -Action (New-ScheduledTaskAction -Execute PowerShell -Argument "Start-process -WindowStyle Hidden PowerShell '(New-Object System.Media.SoundPlayer C:\Windows\Jjgw.wav).PlaySync()'") -Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1)) -f;(New-Object System.Net.WebClient).DownloadFile('https://github.com/User/Repository/raw/master/Music.wav','C:\Windows\Music.wav')"
But i just can't setup single/double quotes to go to the last, instead to closest quotes.
I am trying to achive something like this:
PowerShell ""command1";"command2""
To just execute these two commands.


Solution

  • This is asked often. The best way is to use an EncodedCommand:

    $command = {
        $action = New-ScheduledTaskAction -Execute PowerShell -Argument "Start-process -WindowStyle Hidden PowerShell '(New-Object System.Media.SoundPlayer C:\Windows\Jjgw.wav).PlaySync()'"
        $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1)
        Register-ScheduledTask Regedit -Action $action -Trigger $trigger -Force
        (New-Object System.Net.WebClient).DownloadFile('https://github.com/User/Repository/raw/master/Music.wav','C:\Windows\Music.wav')
    }
    $encodedCommand = [convert]::ToBase64String([System.Text.encoding]::Unicode.GetBytes($command.ToString())) 
    Start-Process -WindowStyle Hidden PowerShell -ArgumentList "-EncodedCommand", $encodedCommand