0

I am trying to run the powershell command "compress-archive" from within a batch file using

setlocal
cd %~dp0
powershell Compress-Archive "-current directory of batchfile" "%USERPROFILE%\Desktop\Test.zip"

This works if I use the literal directories in the paths but I want this to be usable for anyone no matter what their root directory or user profile name is. How can I write this command so that the batch file will execute the powershell command and get the working directory of the batch file and output to the users desktop? I have been searching this site but I cant quite find the answer to this specific situation.

Aaron
  • 63
  • 4
  • do this ENTIRELY in powershell ... and use the `$PSScriptRoot` variable to get the dir that the script is in. if you want the "current dir" instead, use the `Get-Location` cmdlet OR the `$PWD` Present Working Directory automatic variable. ///// in any case DO NOT needlessly mix BAT/CMD with PoSh. [*grin*] – Lee_Dailey Oct 19 '20 at 01:00
  • Thanks for the reply. Based on your advice I was able to use automatic variables and they worked great. I used the command line:powershell Compress-Archive "$PWD" "$env:USERPROFILE\Desktop\Server.zip" – Aaron Oct 19 '20 at 01:17
  • kool! glad to have helped ... and to know that you got it working as wanted! [*grin*] – Lee_Dailey Oct 19 '20 at 02:47
  • Just for information although you think the issue is solved. `cd %~dp0` does not work, for example, for batch file stored in `C:\Temp\Development & Test`. There would be required `cd /D "%~dp0"` which is much better, but still not working if batch file is stored not on a local drive and is run by using UNC path. In this case it would be required to use `setlocal EnableExtensions DisableDelayedExpansion` to define the required environment and `pushd "%~dp0"` to make the directory of the batch file the current directory with `popd` and `endlocal` at end of the batch file. – Mofi Oct 19 '20 at 06:18
  • `%USERPROFILE%\Desktop` is not good as this is just the default for the user's desktop folder. Every user has the freedom to define a different directory as desktop directory. I recommend to take a look on [%USERPROFILE%\Desktop no longer valid after relocating Desktop folder to OneDrive](https://stackoverflow.com/questions/64256571/). It is very easy with PowerShell to get the real user's desktop folder. – Mofi Oct 19 '20 at 06:23

0 Answers0