I am pretty sure that Windows 10 does not support tar file extraction by default.
1. Fast, easy solution for Windows 10
If you expect the script to have access to an active Internet connection, you can simply get 7Zip support using the built-in package manager. This solution:
- Fully automated with a script (no user interaction required)
- Does not require administrator rights
- Installs the 7Zip support module only if it is not yet available.
Here:
function Expand-Tar($tarFile, $dest) { if (-not (Get-Command Expand-7Zip -ErrorAction Ignore)) { Install-Package -Scope CurrentUser -Force 7Zip4PowerShell > $null } Expand-7Zip $tarFile $dest }
To use this:
Expand-Tar archive.tar dest
He does his job, but makes constant changes to the client system. There is a better but slightly more complicated solution:
2. The best solution
The best solution is to link 7Zip4PowerShell with your script. This solution:
- Fully automated with a script (no user interaction required)
- No administrative privileges required
- Does not install any software on the client system
- No internet connection required
- Must work on earlier versions of Windows
but. Download a copy of the 7Zip4PowerShell package
It is distributed under the LGPL-2.1 license, and therefore everything is in order to associate the package with your product. You must add a note to your documentation that it uses the package and provide a link, as you need to provide source code access for licensed GNU products.
Save-Module -Name 7Zip4Powershell -Path .
This will load it into the current directory. This is easiest to do in Windows 10, but there are instructions on how to add PowerShell Gallery to your system in earlier versions of Windows, from the link above.
b. Import the module when you need to extract the tar file
I made a simple function to demonstrate how you can do this based on the first solution:
function Expand-Tar($tarFile, $dest) { $pathToModule = ".\7Zip4Powershell\1.9.0\7Zip4PowerShell.psd1" if (-not (Get-Command Expand-7Zip -ErrorAction Ignore)) { Import-Module $pathToModule } Expand-7Zip $tarFile $dest }
You will need to set up $pathToModule where the linked module is actually stored when your script runs, but you need to understand this. I wrote this example so that you can simply paste both blocks of code above into the PowerShell window and get a working Expand-Tar cmdlet:
Expand-Tar archive.tar dest
Don cruickshank
source share