PowerShell start 'D:\Matthew's scripts\Testing.cmd'

  1. Matthew Wai's Avatar
    Posts : 5,043
    Windows 10 Home 20H2
       #1

    PowerShell start 'D:\Matthew's scripts\Testing.cmd'


    PowerShell start 'D:\Matthew's scripts\Testing.cmd'

    The above does not work because the path contains an apostrophe.
    The following works because the path contains no apostrophe.

    PowerShell start 'D:\Matthews scripts\Testing.cmd'

    Is it possible to fix the "apostrophe" problem? I aim to run the following:
    PowerShell start '<a path containing an apostrophe>' -verb RunAs
      My Computer

  2. dalchina's Avatar
    Posts : 29,927
    Win 10 Pro (1903)
       #2

    Hi, the discussion here:
    What is the literal escape character in Powershell? - Server Fault
    discusses escaping single quotes and an apparent bug or restriction.

    You might also consider which version of Powershell you have:
    How to Upgrade to PowerShell 7 - Petri
      My Computers

  3. Matthew Wai's Avatar
    Posts : 5,043
    Windows 10 Home 20H2
    Thread Starter
       #3

    Thank you very much! The following works. Three double quotation marks are actually necessary for escaping an apostrophe (or a single quotation mark) in a file path:

    PowerShell start """D:\Matthew's scripts\Testing.cmd"""

    The same trick is used below:

    (Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)

    That will automatically get administrative privileges for a CMD file containing the above line.
    It works even if the file name/path contains apostrophes and spaces.
      My Computer

  4. dalchina's Avatar
    Posts : 29,927
    Win 10 Pro (1903)
       #4

    That's an impressive number of """ !
      My Computers

  5. Matthew Wai's Avatar
    Posts : 5,043
    Windows 10 Home 20H2
    Thread Starter
       #5

    dalchina said:
    That's an impressive number of """ !
    Another example: can someone help me combine these two .reg into a context menu flyout?
      My Computer

  6. Try3's Avatar
    Posts : 7,432
    Windows 10 Home x64 Version 20H2 Build 19042.868
       #6

    Matthew,

    And this is the source for that interesting ServerFault article
    PS about_Quoting_Rules - Microsoft Docs

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:51.
Find Us




Windows 10 Forums