Monday, March 19, 2018

[PowerShell] Download file and unzip | 下載檔案與解壓縮

PowerShell is an useful command tool on Windows, here I will show how to download file and unzip it.
PowerShell是個在Windows上很好用的指令列工具,這邊我將展示如何下載檔案和解壓縮。

Download | 下載

We using Invoke-WebRequest to download file in Windows(like wget in linux), the command like this:
我們使用Invoke-WebRequest來在Windows下載檔案(像linux的wget),指令如下:
Invoke-WebRequest -Uri <Uri> -OutFile <String>

By default PowerShell uses TLS 1.0, if your file host's SSL using TLS 1.2, you will got this this error:
PowerShell預設使用TLS 1.0,如果你檔案網站的SSL使用TLS 1.2,你會遇到下面這個錯誤:
Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.

For this case, you can set PowerShell use TLS 1.2 first to avoid the error by this way.
在這情況,你可以用下面方式設定PowerShell優先使用TLS 1.2來避免這個問題。
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
Invoke-WebRequest -uri https://died.github.io/us-map.zip -outfile map.zip


Unzip | 解壓縮

The command Expand-Archive can help us unzip file, usage like this:
指令Expand-Archive可以幫我們解壓縮檔案,使用方式如下:
Expand-Archive <ZipFile> -DestinationPath <String>

Example | 範例 :
Expand-Archive .\map.zip -DestinationPath .\map\

No comments:

Post a Comment