Double quotes inside double quotes issue

Welcome Forums General PowerShell Q&A Double quotes inside double quotes issue

  • This topic has 3 replies, 3 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #221235
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      I need to run this code in command prompt. The problem is that I need to use double quotes for $env:windir path but that doesn’t work. Is there a workaround?

      "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -command "& {$Taskpath = "$ENV:windir\system32\Tasks";$TasksToday = Get-ChildItem -Path $TaskPath -RecurseĀ  ;}"

       

    • #221241
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      I know it may sound silly but to “escape” double quotes you simply wrap them in double quotes. šŸ˜‰

      "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -command "& {$Taskpath = """$ENV:windir\system32\Tasks""";$TasksToday = Get-ChildItem -Path $TaskPath -Recurse  }"

      BTW: When you post code or error messages or sample data or console output format it as code using the code tags “PRE” you find in the edit bar in the “Text” view, please. When you use the “Visual” view you can use the format template “Preformatted”. Thanks in advance.

    • #221289
      js
      Participant
      Topics: 29
      Replies: 810
      Points: 2,432
      Helping Hand
      Rank: Community Hero

      Or make it a sum of two strings:

      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -command "& {$Taskpath = $ENV:windir + '\system32\Tasks';$TasksToday = Get-ChildItem -Path $TaskPath -Recurse}"

      Assuming you’re in cmd:

      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile ^
        -command $Taskpath = $ENV:windir + '\system32\Tasks'; $TasksToday = ^
        Get-ChildItem -Path $TaskPath -Recurse

      Or

      powershell -nop $taskstoday = dir $env:windir\tasks -r
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
    • #221328
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      Great! Thank you Olaf & Js. Both solutions work.

      @Olaf, I will follow your directions about posting.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.