When trying to unzip files traditionally in Windows, it unzips to the name of the zip file as a folder with the files embedded into the folder itself. I am unable to recreate this using the Extract-Archive Cmdlet. Here is the code section for this that I have:
Get-ChildItem 'path of zip' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force
While running this, the unzip process works fine. I am just wondering if there is a way to keep the traditional unzip file/folder structure when using the Expand-Archive Cmdlet
Thank you
You can set the destination folder in parameter DestinationPath. If the folder with the name of the zipfile does not yet exist, it will be created:
$sourcePath = 'path of zip file(s)'
$destination = 'path to extract'
Get-ChildItem -Path $sourcePath -Filter '*.zip' -File | ForEach-Object {
$target = Join-Path -Path $destination -ChildPath $_.BaseName
$_ | Expand-Archive -DestinationPath $target -Force
}
Related
$Path = Get-Location | Select -expand Path
Set-Location ..
Remove-Item -LiteralPath "FolderLocation" -Recurse -Force
This isn't working when I execute the powershell file through .cmd file.
What about:
$Path = Get-Location
Set-Location ..
Remove-Item -Path $Path -Recurse -Force
This worked for me but I did not have any files or folders in that folder.
This is what I am trying to do with powershell for zipping files.
Sort out files older than xxx days
Add those files in a zip file.
Delete the source files.
I have Powershell_Community_Extensions installed so I use Write-zip to do the job.
$Source = "\\network\share"
$files = Get-ChildItem -Path $Source | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-62)}
$files | Write-Zip -OutputPath $Source\Archive.zip -EntryPathRoot $Source -Append -Quiet
Remove-Item $files -Force
Issues:
I have to use -EntryPathRoot with Write-zip, otherwise it wont pick up network share
Remove-item also wont pickup files from network share, it says "Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist.", why it deleting file from C:\Windows\system32\ instead of \\network\share
Write-zip -append did add files into the zip file, but it did not just add files in the root of that zip file, it actually created entire folder structure in the root of the zip and added newer filtered files in the end of that folder structure. I just want to add the newer filtered files into root of that zip file.
Any idea please?
Utilizing the v5 *Archive cmdlets:
$Source = '\\network\share'
$Files = Get-ChildItem -Path $Source |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-62) }
Compress-Archive -Path $Files.FullName -DestinationPath $Source\Archive.zip -CompressionLevel Optimal -Update
$Files | Remove-Item -Force
I have a source folder(c:\test*) and I want that folder copied to all folders in the same location (c:\resultaat) but only to the folders in that location that contains the name demo_profit.
I think I'm almost there, but I'm missing a small part I think.
$destination = get-item -include demo_profit* -path C:\resultaat\*
copy-item C:\test\* $destination -recurse -force
thanks for your help
The -Directory parameter of Get-ChildItem requires AFAIK PSv5
Get-ChildItem -Directory -Path 'C:\resultaat\' | ForEach-Object{
if (Test-Path (Join-Path $_.FullName 'demo_profit')){
Copy-Item C:\test\* $_.FullName -recurse -force
}
So it is not clear if you want to copy the files to a folder containing an item named demo_profit, or to folder with demo_profit in the name. The second is simple, so I'll start there:
Get-ChildItem C:\resultaat\* -Directory |
Where{$_.Name -match 'demo_profit'} |
ForEach{ Copy-Item C:\test\* -dest $_.FullName -recurse -force }
That finds any folder within c:\resultaat that has 'demo_profit' as part of its name, and copies the desired files/folders into that folder. Example: C:\resultaat\Folder1_demo_profit would have all the files from C:\temp copied into it.
If you are looking for things named demo_profit, and want to copy the files into the same folder as that item you need to use the PSParentPath property, and some wildcards.
Get-ChildItem c:\resultaat\*\*demo_profit*
This command would find a file or folder with a path such as 'C:\resultaat\Amazon\az_demo_profit'. Then to copy files into the folder containing that you would use the PSParentPath property as such:
Get-ChildItem c:\resultaat\*\*demo_profit* |
Select -Expand PSParentPath -Unique
ForEach{ Copy-Item C:\test\* -dest $_ -recurse -force }
I am trying to copy a folder from the local computer to a remote server. It works but if the destination folder already exists it is creating a duplicate folder inside it.
copy-item -Path C:\test -Destination \\server\F$\testpassed -recurse -Force
To copy only the files from within C:\test to the \\server\F$\testpassed folder you need to use the following command:
Copy-Item -Path C:\test\* -Destination \\server\F$\testpassed -Recurse
\* is a wildcard for anything within the folder, and will cause Copy-Item to copy anything within the folder to the Destination. You could also use *.txt to only copy txt files if you wanted only a specific file type to be copied.
EDIT:
I would test for the presence of $TARGETDIR and then create it if needed. This way you only have a single copy command.
$TargetDir = "\\server\F$\testpassed"
$SourceDir = "C:\test"
if(!(Test-Path -Path $TARGETDIR)) {New-Item -Path $TARGETDIR -ItemType Directory}
Copy-Item -Path "$SourceDir\*" -Destination $TARGETDIR -Recurse
Using source path in below way will solve your issue
Copy-Item -Path C:\test*
Try
$Source = Get-childitem C:\test -Recurse
copy-item -Path $Source.FullName -Destination C:\temp -recurse -Force
Use GC to stop getting the folder as well as the contents.
My folder C:\Downloads\Files\ has 3 .zip files.
test1.zip
test2.zip
test3.zip
I need to copy these files into same location and append another extension .dat to the file.
The total files in my folder C:\Downloads\Files\ should have 6 files now (3 .zip and 3.dat)
test1.zip
test2.zip
test3.zip
test1.zip.dat
test2.zip.dat
test3.zip.dat
Can anyone help me get this done?
Use the Get-ChildItem cmdlet to retrieve all zip files and copy them using the Copy-Item cmdlet:
Get-ChildItem -Path 'C:\Downloads\Files\' -Filter '*.zip' |
Copy-Item -Destination { "$($_.Name).dat" }
The following copies only files from within the $path directory with extension .dat.
Get-ChildItem -Path $path -File | ForEach-Object {Copy-Item -Path $_.FullName -Destination "$($_.FullName).dat"}