none
%~dp0 in Powershell RRS feed

  • Question

  • I have created a simple PowerShell script that uninstalls Firefox using the helper.exe file and would like to install the new version (exe) after the fact. In the past my bat file would read.

    "%~dp0Firefox Setup 60.0" -ms
    if exist "C:\Program Files\Mozilla Firefox\" copy /Y "%~dp0override.ini" "C:\Program Files\Mozilla Firefox\browser\"
    if exist "C:\Program Files\Mozilla Firefox\" copy /Y "%~dp0mozilla.cfg" "C:\Program Files\Mozilla Firefox\"
    if exist "C:\Program Files\Mozilla Firefox\" copy /Y "%~dp0local-settings.js" "C:\Program Files\Mozilla Firefox\defaults\pref"

    I'm having difficulty figuring out the PowerShell equivalent.


    DHeinz

    Wednesday, May 23, 2018 1:34 PM

Answers

  • Here's how to do the research and find out the answer.

    What does %~dp0 do in cmd.exe? Read the help - for /? - and you will find out that %~dx means "drive letter for replaceable parameter %x," and %~px means "path for replaceable parameter %x."

    So what's replaceable parameter %0? It's the path and filename of the currently running shell script (batch file). You can find this out all by yourself by creating a batch file that contains a single line:


    @echo %0

    Exercise: OK. So what's the next question we need to ask? How about "How do I find out the directory name of a running script in PowerShell?"

    PowerShell has automatic variables also. Let's look in the help topic and see if there is anything useful:


    PS C:\> help about_Automatic_Variables

    The $PSScriptRoot variable looks promising:

    $PSScriptRoot - Contains the directory from which a script is being run.

    So there you go. The PowerShell equivalent of %~dp0 is $PSScriptRoot.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 23, 2018 2:17 PM
    Moderator

All replies

  • "$pwd\dp0override.ini"


    \_(ツ)_/

    Wednesday, May 23, 2018 1:47 PM
  • Here's how to do the research and find out the answer.

    What does %~dp0 do in cmd.exe? Read the help - for /? - and you will find out that %~dx means "drive letter for replaceable parameter %x," and %~px means "path for replaceable parameter %x."

    So what's replaceable parameter %0? It's the path and filename of the currently running shell script (batch file). You can find this out all by yourself by creating a batch file that contains a single line:


    @echo %0

    Exercise: OK. So what's the next question we need to ask? How about "How do I find out the directory name of a running script in PowerShell?"

    PowerShell has automatic variables also. Let's look in the help topic and see if there is anything useful:


    PS C:\> help about_Automatic_Variables

    The $PSScriptRoot variable looks promising:

    $PSScriptRoot - Contains the directory from which a script is being run.

    So there you go. The PowerShell equivalent of %~dp0 is $PSScriptRoot.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 23, 2018 2:17 PM
    Moderator
  • Is this the same if running v2?

    DHeinz

    Wednesday, May 23, 2018 2:31 PM
  • Why ask when you can do a quick test?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 23, 2018 2:45 PM
    Moderator
  • Is this the same if running v2?

    DHeinz

    Is "what" the same thing?

    No one should be running V2 for serious security issues.  V2 should be removed from all systems that support removal (W8 and later).

    Shell macros do not work in any version of PowerShell.

    Bill's suggestion to use the shell help for this will be useful as you learn PowerShell.


    \_(ツ)_/

    Wednesday, May 23, 2018 3:00 PM
  • The point is that there's no need to ask these kinds of questions because you can research and test all by yourself. This has a number of advantages; including, but not limited to:

    • You get to learn the material on your own and retain this knowledge for future use. (This can even be fun!)
    • It helps you to ask better questions instead of asking others to do research and testing for you.
    • You get to an answer much more quickly than waiting for someone to respond in an online forum.
    • You will be proud of yourself that you were able to find answers on your own and you will be able to help others with this knowledge.
     

    Learn how to research and test. Verify answers you are given. There is no need to be helpless.


    -- Bill Stewart [Bill_Stewart]


    Wednesday, May 23, 2018 4:18 PM
    Moderator
  • $PSScriptRoot was introduced in PowerShell 3.0.

    In v2 you can try this:

    $ScriptRoot = Split-Path $MyInvocation.MyCommand.Path –Parent

    But you really should upgrade PowerShell.

    Wednesday, May 23, 2018 9:08 PM