-->

How to properly set path in Powershell and 7zip?

2019-08-09 08:11发布

问题:

I have a Powershell script to create a self-extracting archive via 7zip. But it's receiving this error:

cannot find specified SFX module

The Powershell code is:

set-alias sz "$env:ProgramFiles\7-Zip\7z.exe
sz a -t7z -sfx -ppassword $fullpath $filetostore

Both variables are valid. I've tried -sfx and -sfx7z.sfx, same error. The 7z.sfx file is indeed in the correct folder with 7zip. I can also verify the alias is working, as the 7zip copyright appears when running the code (so 7zip commandline is being initiated). This command works outside Powershell.

I'm also tried Set-Location into the 7zip folder, but same error. What am I missing?

回答1:

It seems you should add the 7-zip folder to your PATH environment variable to make things easier :

#find the 7-zip folder path
$7zPath = (Get-ChildItem "C:\Program Files","C:\Program Files (x86)" -Include "7-zip" -Recurse -ErrorAction SilentlyContinue).FullName

#add it to PATH environment variable
$env:Path += ";$7zPath;"

Then you can run 7z -sfx with no errors about the SFX module.