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


  1. Posts : 7,607
    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. Posts : 42,999
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #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. Posts : 7,607
    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. Posts : 42,999
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #4

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


  5. Posts : 7,607
    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. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #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 00:40.
Find Us




Windows 10 Forums