%~dp0 equivalent in powershell (using Expand-Archive cmdlet)


I’m pretty new to scripting (especially powershell) and new to Stack Overflow, so please, excuse my ignorance and please bear with me! I will do my best to specifically explain what I’m looking to do and hopefully someone can give a detailed response of what I could do to make it work..

Intended Process/Work Flow: A co-worker downloads “Install.zip” file that has all the necessary files. This “Install.zip” file contains “Setup.bat” file (for computer config), “Fubar.zip” file, 2 powershell scripts, and a custom powerplan (.pow) file. Once downloaded they will run the “Setup.bat” file and it will pretty much do all the work. Inside that batch file it calls 2 powershell scripts. 1)”Download.ps1″ – Downloads some other files from the web. 2.) “Unzip.ps1” – Unzips “Fubar.zip” and places contents in another folder – C:\TEST\

Issue: I’ve recently gotten familiar with using %~dp0 in batch files. I want to make sure that the location where my co-worker initially downloads the Install.zip doesn’t throw off my batch file. So for example.. some people will download .zip files to the “Downloads” folder, then extract contents to proper destination. Others will download the .zip to a specific folder, then extract it within that folder. [Ex: C:\Alex\Install.zip –Extraction– C:\Alex\Install\((Content))] So I tried to not pre-define file locations due to the variables. I’ve gotten the %~dp0 to work everywhere I need it to in my batch file. The only issue I have is getting my powershell scripts to use same working directory that my batch file is in. *My batch file and my powershell scripts will always be in the same directory. (Wherever that may be)

Goal: I want my powershell script (“Unzip.ps1”) to look for my “Fubar.zip” file in the same directory that its currently running in. (Again – Wherever that may be) I basically want to remove any variables that may throw off the powershell script. I want it to always use it’s current working directory to locate Fubar.zip. I basically need powershell to either use its current working directory OR figure out a way to have it pull its current working directory and use that to look for “Fubar.zip”.

my current “Unzip.ps1” powershell script is extremely basic.

Unzip.ps1:Expand-Archive -Force c:\ALEX\Install.zip\Fubar.zip -dest c:\TEST\

Batch File Command that calls the Unzip.ps1 script: Powershell.exe -executionpolicy remotesigned -File %~dp0UNZIP.ps1

Please keep in mind, I’m just learning scripting and I’m teaching myself. My knowledge is limited, but I’ve made it this far and this is the only part I’m stuck on. Please give clear responses. Any help or advice would be extremely appreciated! Using PowerShell 5.0

Thanks in advance!


From your description, I gather that Fubar.zip and Unzip.ps1 are in the same directory. We’ll pretend this directory is C:\Users\Me\Temp; although I understand that may vary.

Powershell’s working directory will be the directory you’re in (if called from CMD) when you launch; otherwise, it’ll be from $env:UserProfile. Since the .bat file always call Unzip.ps1 from the directory that it’s in (C:\Users\Me\Temp), powershell.exe will find it with this command (you can still use %~dp0 here; it’s not hurting anything):

Inside of Unzip.ps1, you’ll use Get-Location:

However, if the .bat file does a cd into another directory, this won’t work. From your %~dp0UNZIP.ps1 example, I assume this isn’t the case, but let’s address it anyway. If this is the case, you need to process from where the location of the script is. So for this call the full/relational path to the .ps1:

Then, your Unzip.ps1 will need to look like this:

Alternatively, you can also do some fancy path splitting, as @JosefZ suggested. The $PSCommandPath and $MyInvocation variables contain the full path to your script; which you should familiarize yourself with:

Note: Of course, you wouldn’t set $location twice. I’m just showing you two ways to set it.

I hope this helps!


%~dp0 equivalent in powershell (using Expand-Archive cmdlet) by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply