Windows 10 - how to compress file without creating a parent folder - windows

In Windows 10,
from power shell, I want to compress a folder without it creating a parent folder inside the zip file
currently I use:
Compress-Archive -Path . ../abc.zip
Is it possible to create the archive without the parent folder?

You might try this (didn't test it and I don't even have any window installed, so might not work):
gci c:\folder\* | Compress-Archive -DestinationPath c:\abc.zip

Related

How do I reference multiple paths in Powershell?

I am writing a script that pulls from multiple directories located at the root folder of the script. When I use this:
$ScriptPath="$PSScriptRoot\Scripts"
$BinaryPath="$PSScriptRoot\Binaries"
$DataFolderPath="$PSScriptRoot\Data"
PowerShell complains about 2 of the 3 paths saying they can't be found.
I also tried this but no luck.
$ScriptPath=Join-Path (Split-Path $PSScriptRoot) -ChildPath Scripts
$BinaryPath=Join-Path (Split-Path $PSScriptRoot) -ChildPath Binaries
$DataFolderPath=Join-Path (Split-Path $PSScriptRoot) -ChildPath Data
I would try:
$ScriptPath=$PSScriptRoot+"\Scripts"
$BinaryPath=$PSScriptRoot+"\Binaries"
$DataFolderPath=$PSScriptRoot+"\Data"
How are you running this?
This...$PSScriptRoot, will only be populated when you run the full script, not when you run this in the ISE/VSCode, or other editor pane window.
Lastly, $PSScriptRoot, as documented...
About Automatic Variables
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1
PSScriptRoot Contains the full path to the script that invoked
... thus, if you are not in a source directory with that set of named subfolders, then you should expect this to fail.
If you want this to work, then you must check for the existence of the subfolders before you begin, and if not already there, then, your script should create them, then run the rest of your code.

How to expand ZIP archive using PowerShell (UTF-8 filenames)

My zip archive has a single file:
Père-Noël.txt
The zip expands nicely with Windows File Explorer, 7-Zip or any other tool I've tried. But I cannot figure out how to do it from PowerShell. Obviously I've tried Expand-Archive but it cannot handle the file name and garbles it into PŠre-N”el.txt. Note: The problem isn't specifically with this example, but indeed with any file name which uses characters outside of the ASCII-127 range. Or so it seems.
Any solution which uses PowerShell and which doesn't rely on an external tool - whose presence cannot be guaranteed - will be accepted. Windows 10 is the platform. I cannot do system-level changes and cannot rely on users of the script having any specific global setting on their system. It has to be a solution within the script.
Is there another way, besides Expand-Archive ? Or is there a setting in PowerShell which will magically do the trick?
Steps to reproduce:
On your Windows 10 host:
Create an empty file named Père-Noël.txt.
ZIP the file using Windows Explorer ("Compressed Folders" feature) into an ZIP archive of your choice, say myarchive.zip.
Delete the Père-Noël.txt file.
Now try to unpack the myarchive.zip using PowerShell. This operation should create the file Père-Noël.txt again.
Compressing using PowerShell Compress-Archive cmdlet
True, if the ZIP was originally created using Compress-Archive cmdlet then it actually works as intended when decompressing using Expand-Archive. So you can say that PowerShell is compatible with itself. It is just not compatible with Windows Explorer ZIPs.
You'll likely need to check the encoding [System.Text.Encoding]::GetEncodings() but the below works with your example Père-Noël
$zipfile = 'C:\test\Père-Noël.zip' #Contains Père-Noël.txt
$outpath = 'C:\test\out'
$enc = [System.Text.Encoding]::GetEncoding(29001) #29001, x-Europa, Europa
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath, $enc)
Hope this helps,
Although we arrived in 2021 I stumbled upon the same problem.
Like the accepted answer my solution is based on the System.IO.Compression namespace.
The expand-archive command accepts pipeline-input and a -Force switch.
I had the same goals for my implementation - still in work and not thoroughly tested.
$encoding = [System.Text.Encoding]::GetEncoding(437)
Write-Output $encoding
Get-ChildItem -Path ".\*.zip" | Unzip -target "C:\unzipped" -f -encoding $encoding -v
Find unzip function here
It's similar to the accepted answer but I like this answer better:
[System.IO.Compression.ZipFile]::ExtractToDirectory("$pwd/test.zip", "$pwd", [System.Text.Encoding]::GetEncoding((Get-Culture).TextInfo.OEMCodePage)
source

Update context menu of all file extensions in Windows via powershell takes long time

Developing our own application for our company only, we have developed script used for installation from shared drive. Except the installation itself, the script should also create/update values in the registry of particular user (HKEY_CURRENT_USER).
These values are separated for:
Directories (HKCU:\Software\Classes\Directory)
All File Extensions (HKCU:\Software\Classes\*)
For the directory folder the update is immediate, where for the extensions it seems to take quite some time depending on machine hardware (from 40 sec to 2 minutes).
Now there is a trouble to create "entry" in the registry for the folder named * only. I've got a question for this to resolve (PowerShell: How do I create selector on file/folder, whose name is '*' (asterisk/star)?).
Ignoring the issue above, we have found some solution how the string path works, however I'm not sure what is happening behind the code and do not understand why it takes so long time.
# Directory
New-Item -Path "HKCU:\Software\classes\Directory" -Name "shell" | Out-Null
# All Files Extension
New-Item -Path "HKCU:\Software\classes\[*]" -Name "shell" | Out-Null
One idea is that the [*] solution actually goes through all the file extensions, but the registry itself is showing this NewItem under * folder and not shown under particular extensions:
Another idea about this, is when we have a registry file (*.reg), by running the file the registry entry is added immediately and resolve the case.
Questions:
What is actually happening when we are running the query to add entry under [*] selector?
How can be this process optimized to lower the time for creating new folder in registry for all files' extension?
I suspect what's happening is that the -Path in your New-Item call is recursive because of the wildcard. Hence the delay.
Here's a workaround to the issue:
Set-Location -LiteralPath "HKCU:\Software\classes\*"
New-Item -Name "shell"
New-Item uses the current location as the -Path if not explicitly passed to the function.

How to use short-cut paths to Compress-Archive to zip current folder into same destination

I am using Compress-Archive and want to zip the current directory into the same path. However I do not want to have to type out the entire file path both times. Is there an easy way to do this?
I am using windows 10 pro.
This works for the most part Compress-Archive . test.zip but I want it to be on the same level as the current directory so I need to put it back one spot.
Something like this is what I want:
path/test
path/test.zip
What I am getting:
path/test
path/test/test.zip
It is going inside the actual folder which is not what I want
You propably want that:
Compress-Archive * ..\test.zip
The wildcard * avoids that the name of the folder is put inside the zip.
Using .. for the output path we go one level up in the directory tree.
This command will fail if test.zip already exists. Either add parameter -update to update the archive or add -force to overwrite the archive. Both can be used even if the archive does not already exist.
If the current working directory is "t", it can be included using the following command. I would note that I do not think putting the destination .zip file in the directory being compressed is a good idea.
Compress-Archive -Path $(Get-ChildItem -Recurse -Exclude t.zip) -DestinationPath .\t.zip -Force
It is shorter if you are willing to use aliases and cryptic switches.
Compress-Archive $(gci -r -e t.zip) .\t.zip -Force
If I have misinterpreted your situation, please leave a comment or improve the information provided by editing the question.

Copy nested folder structures

I am copying over all items from one directory to another that has files and subfolders with files using this command I am using is simple:
(Get-ChildItem -Recurse -Path $source).ForEach({$_ | Copy-Item -Destination $dest})
It works but the problem is that it doesn't copy files within subfolders to the destination subfolders they belong but to the main destination folder instead. Wondering if I can I modify this command in a concise way so it's still a one-liner and does that?
The difference between this and similar questions is that I have nested folder structure with sub-folders and files within them.

Resources