Escape quotes with cmd

Mansouri, Sadok 21 Reputation points
2022-05-12T08:14:19.42+00:00

Hello guys,

i want to run this powershell scripblock with cmd script :

Get-ScheduledTask | foreach {Export-ScheduledTask -TaskName $.TaskName -TaskPath $.TaskPath | Out-File (Join-Path “D:\Testing\Sauv\SchTasks” “$($_.TaskName).xml”)}

Like :

powershell -command “Get-ScheduledTask | foreach {Export-ScheduledTask -TaskName $.TaskName -TaskPath $.TaskPath | Out-File (Join-Path “D:\Testing\Sauv\SchTasks” “$($_.TaskName).xml”)}”

but i can’t get the result, how can i espace the quotes ?

thanx for your help

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,466 Reputation points
    2022-05-12T15:07:22.84+00:00

    Microsoft has a handy tool that helps you to construct (well, actually it just shows you the results; it doesn't offer any help) command line quoting. You'll find the source code here: TestExe. You'll have to compile it yourself, but you can install the free version of Visual Studio to accomplish that.

    After correcting the "smart quotes" and restoring the missing "underbar" characters in your post, here is the results from testing your original string:

    "Get-ScheduledTask | foreach {Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-File (Join-Path "D:\Testing\Sauv\SchTasks" "$($_.TaskName).xml")}"
    
    Arg 0 is <Get-ScheduledTask | foreach {Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-File (Join-Path D:\Testing\Sauv\SchTasks $($_.TaskName).xml)}>
    

    Doubling the double quotes within the data, the results look like this:

    "Get-ScheduledTask | foreach {Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-File (Join-Path ""D:\Testing\Sauv\SchTasks"" ""$($_.TaskName).xml"")}"
    
    Arg 0 is <Get-ScheduledTask | foreach {Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-File (Join-Path "D:\Testing\Sauv\SchTasks" "$($_.TaskName).xml")}>
    

    I think that'll get you what you want.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.