$PSScriptRoot & %~dp0: Script's Home
When writing portable scripts, one of the first challenges is locating files relative to the script itself. Whether in PowerShell or a classic Batch file, you need a reliable way to find your script’s “home” directory. This guide breaks down the two most important tools for this job: $PSScriptRoot for PowerShell and %~dp0 for Batch. 1. The Batch Method cd /d %~dp0 In Windows Batch scripting (.bat, .cmd), the magic variable %~dp0 is the standard for getting the script’s directory. It’s often used with cd to change the working directory. ...