I am having some problems with my powershell script to unzip a zipped folder. Any one have any solutions? I have searched the net but everyone seem to be using powershell v1.0 and above.
Does anyone have a solution to unzip a zipped folder for powershell 1.0?
Thanks in advance!
I don't have a Powershell 1 to test with (who does these days!) but I found this snippet on my machine from the days before Expand-Archive was available:
# Unzip stuff
$shell = new-object -com shell.application
$zip_file = get-item "C:\temp\c1.zip"
$Location = $shell.namespace("C:\temp")
$zipFolder = $shell.namespace($zip_file.fullname)
$Location.Copyhere($zipFolder.items())
It will unzip the file to the folder it is in. I believe it should work with PS1, but you'll have to test it yourself.
Related
I am currently using PSDeploy to deploy my PowerShell script modules to a local PowerShell gallery and it is working great for that. It also works for deploying single files such as my profile.ps1.
Now I am wondering if I can use PSDeploy to deploy other non-PowerShell script files such as my AutoHotkey scripts. For the AHK scripts, the deploy task would only be copying a folder, including script files, from my dev location to a location where I run the scripts.
Unfortunately I keep getting an error while attempting to deploy a folder of script files with PSDeploy.
I created a simple test project on Github to help toubleshoot the error. Anyone can clone this project locally and run a test like I am if they want to.
My test PSDeploy script looks like this:
Deploy FolderTest {
By FileSystem {
FromSource '.\deployFolderFrom'
To '.\deployFolderTo'
}
}
This is how I am running my tests.
# I am running my test in 'G:\_temp'
cd G:\_temp
# Clone the test project to your test folder or in my case 'G:\_temp'
git clone https://github.com/jesmith01/PSDeployTest.git
# Change directory to the PSDeployTest folder.
cd .\PSDeployTest\
# Run this command from PSDeployTest folder.
Invoke-PSDeploy -Verbose
This is my results with verbose setting turned on, error at the end:
My PowerShell version is 7.2.1 as of running this test:
My OS version is Windows 11:
I even tested out this example code from RamblingCookieMonster exactly, without changing anything, and I still get an error:
# Source folders and files
mkdir C:\PSDeployFrom
mkdir C:\PSDeployFrom\MyModule
mkdir C:\PSDeployFrom\SomeScripts
mkdir C:\PSDeployFrom\Deployments
New-Item -ItemType File -Path C:\PSDeployFrom\MyModule\MyModule.psm1
New-Item -ItemType File -Path C:\PSDeployFrom\MyModule\MyModule.psd1
New-Item -ItemType File -Path C:\PSDeployFrom\SomeScripts\Script1.ps1
New-Item -ItemType File -Path C:\PSDeployFrom\SomeScripts\Script2.ps1
# Target folder, with a file that mirror should overwrite
mkdir C:\PSDeployTo
mkdir C:\PSDeployTo\MyModule
New-Item -ItemType File -Path C:\PSDeployTo\MyModule\Remnant.ps1
# Sample PSDeploy.ps1 file
Set-Content C:\PSDeployFrom\Deployments\my.psdeploy.ps1 -Value #'
Deploy ExampleDeployment {
By FileSystem Scripts {
FromSource 'SomeScripts'
To 'C:\PSDeployTo'
Tagged Dev
DependingOn ExampleDeployment-Modules
}
By FileSystem Modules {
FromSource MyModule
To C:\PSDeployTo
Tagged Prod, Module
WithOptions #{
Mirror = $true
}
}
}
'#
Set-Location -Path 'C:\PSDeployFrom'
After running the above sample code:
# Run this command from 'C:\PSDeployFrom' folder.
Invoke-PSDeploy -Verbose
This is my results with verbose setting turned on, getting the same error at the end:
I reviewed a bunch of *.psdeploy.ps1 scripts from others on GitHub, read the documentation, tested out examples from RamblingCookieMonster, tried using absolute paths, relative paths and I even took a peek at the PSDeploy source code where I am getting the error. I still cannot figure out where this error is coming from. It appears what I am doing is right.
Q: Is PSDeploy only intended for deploying PowerShell script modules or can it also be used to deploy non-PowerShell folders and files?
Q: Is it possible my PSDeploy installation is faulty or maybe it is not compatible with PowerShell 7.2.1?
Any help is appreciated. Thanks.
UPDATE: Thanks to #MathiasR.Jessen comment, he gave me the idea to try the same tests with Windows PowerShell 5.1 and they all worked! For now I can just use Windows PowerShell for deployment until I figure out what is broken with PowerShell 7.2.1.
Works with Windows PowerShell 5.1
I can create a Scheduled Task in Windows using the Register-ScheduledTask PowerShell cmdlet with a particular path (i.e., using -TaskPath "\SomePath\" and -TaskName "SomeName"). I can delete the Task using the Unregister-ScheduledTask with the same parameters. I can verify the deletion with the Windows "Task Scheduler" GUI. However, the folder ("\SomePath\") does not get deleted. While this makes perfect sense, I cannot find any way to delete this empty folder.
How do I delete an empty Task Scheduler folder using PowerShell?
UPDATE: After more research, I found another way to solve this problem within PowerShell. It involves working with a Schedule.Service object. Here is the code to delete a folder called 'My Task Folder':
$scheduleObject = New-Object -ComObject Schedule.Service
$scheduleObject.connect()
$rootFolder = $scheduleObject.GetFolder("\")
$rootFolder.DeleteFolder("My Task Folder",$null)
ScheduledTasks are just xml files which are stored here:
Get-ChildItem -Path 'C:\Windows\System32\Tasks'
You can just delete them from there. They are also referenced from the registry here:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache
Just use the...
Remove-Item -Path 'somepath' -Force
... cmdlet as you normally would for any file or folder.
Now of course, if there is a file/folder attribute set, that is preventing the delete, just remove that.
To delete the task folder called "MEGA" in CMD (or BAT file):
powershell -c "$scheduleObject = New-Object -ComObject Schedule.Service; $scheduleObject.connect(); $rootFolder = $scheduleObject.GetFolder('\'); $rootFolder.DeleteFolder('MEGA',$null)"
Thanks to #jrsrjrsr for Powershell commands.
I am trying to extract .exe file using powershell without any other tools.
I tried to use System.IO.Compression.ZipFile, but that works only for .zip files..
$zip_file = Get-Item ("C:\Users\00WORK\gs\gs.exe")
$destination = Get-Item ("C:\Users\tuna")
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip_file,$destination)
Also tried this, but without any success
start-process C:\Users\Downloads\gs.exe -Argumentlist "/a"
Tried also this but once again without any succes
$shell = new-object -com shell.application
$zip = $shell.NameSpace(“C:\Users\00WORK\gs\gs.exe”)
foreach($item in $zip.items())
{
$shell.Namespace(“C:\Users\tuna”).copyhere($item)
}
Thanks for help.
If you have tried
start-process C:\Setup.exe -Argumentlist "/a"
Then it is not possible using powershell, this command is completely dependant on how the file was packaged, if all else fails I personally would use a utility like 7-Zip, but as you said you would not like to use utilities.
I had this same problem recently as well. Dewi's answer would not have worked for me. I solved it like this:
mv gs.exe gs.zip
Expand-Archive -Path gs.zip
Optionally, you can rename it back when you're done, if you need the exe for other purposes.
mv gs.zip gs.exe
Note that you'll need PowerShell 5 or newer to use Expand-Archive. Thankfully, this is available as a download all the way back to Win 7 as Microsoft KB3191566.
Yeah well powershell doesn't recognize all compression algorithms. I am trying to do this with an HP driver pack file which comes in a self extracting .exe file. Even if you change the filename to a .zip it will not extract the files and instead throws a New-Object exception error.
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?
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.
While Sodawillow has an answer that will work for the active session, a more permanent answer would be to add 7zip to the path for the Environment you are working in:
[Environment]::SetEnvironmentVariable("Path",$env:Path+";C:\Program Files\7-zip", [EnvironmentVariableTarget]::User)
The above one-liner should add 7zip to the active user account's path. Change 'User' to 'Machine' for the whole computer, or 'Process' for the currently running window. If you set 'User' or 'Machine', you will need to open a new powershell instance to see the change reflected.
I am fairly new to powershell and I am trying to create a script that executes a .exe file. I can execute them on my machine no problem because the folder path is hard coded. The problem is that if I shift this script to another computer, the .exe it calls might be located in a different folder structure. Example
My computer:
D:\Folder1\subfolder\RunMe.exe
Client computer might be
D:\RunMe\subfolder\RunMe.exe
I just need it to execute the RunMe.exe no matter where it is. Is there a way to do this in powershell?
# 1. Get the location of RunMe.exe
$RunMe = Get-ChildItem -Path d:\* -Include RunMe.exe -Recurse;
# 2. Invoke RunMe.exe
Start-Process -FilePath $RunMe[0].FullName -Wait -NoNewWindow;