powershell7zipwindows-server-2019taskscheduler

Extraction 7zip script doesn't work in Windows Server 2019 Task Scheduler


I have created a ps1 script to extract a 3Gb zip folder. It works Whenever I run it from PowerShell ISE but when I add my script to windows server 2019 task scheduler it just says running and it doesn't do anything.

I tried

set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
Set-location C:\
sz x -oC:/ test.zip

I also tried

setx path "%path%;C:\Program Files\7-Zip"
Set-location C:\
7z x test.zip -oc:\ -r

I expect to extract the 3gbs folder.


Solution

  • I found the solution here: https://web.archive.org/web/20221027040500/https://ridicurious.com/2019/07/29/3-ways-to-unzip-compressed-files-using-powershell/

    I was able to solve it this way:

    Add-Type -Assembly "System.IO.Compression.Filesystem"
    [System.IO.Compression.ZipFile]::ExtractToDirectory('C:\test.zip','C:\')
    

    And the way I have created the Task Scheduler:

    schtasks /create /tn "Extract" /sc onstart /delay 0000:30 /rl highest /ru system /tr "powershell.exe -file C:\scripts\setup\test.ps1"
    

    It took 4 minutes to extract a 3GB folder.