I'm writing a Windows batch script that needs to extract ethernetspeed.zip into a temp directory. I tried calling PowerShell's Shell.Application COM object, but its NameSpace method does not seem to accept my relative paths. Hard-coding paths such as C:temp and C:ethernetspeed.zip works, but the script needs to run from different locations and on other Windows 10 or 11 machines. What is the cleanest way to build absolute paths from the batch file, or otherwise extract the archive without hard-coding them?
3 Answers
For a batch file, %~dp0 expands to the drive and directory containing the batch file. You can pass those absolute paths into PowerShell, for example: `powershell.exe -NoLogo -NoProfile -Command "$root='%~dp0'; Expand-Archive -LiteralPath (Join-Path $root 'ethernetspeed.zip') -DestinationPath (Join-Path $root 'temp') -Force"`. Quote carefully if the script may be stored under a path containing spaces. `Expand-Archive` is available in the Windows PowerShell installation included with supported Windows 10 and 11 systems, and avoids the COM object entirely.
You can also let PowerShell resolve the paths relative to the batch file’s working directory, but that directory is not necessarily where the batch file is located. Using `%~dp0` is safer when the archive and destination are alongside the script. A batch-only version can create the destination and use the built-in tar executable: `mkdir "%~dp0temp" 2>nul` followed by `tar.exe -xf "%~dp0ethernetspeed.zip" -C "%~dp0temp"`. The destination directory must exist first.
If the archive is always next to the batch file, you can also change the working directory at the start with `cd /d "%~dp0"`, then use relative paths in the PowerShell command. However, changing the working directory can affect later commands, so explicitly joining `%~dp0` with each filename is generally clearer and more reliable.

That is the kind of solution I was looking for: keep the batch entry point, but let PowerShell construct the full paths from the script location.