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.

Download | 下載

We using Invoke-WebRequest to download file in Windows(like wget in linux), the command like this:
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 -outfile

Unzip | 解壓縮

The command Expand-Archive can help us unzip file, usage like this:
Expand-Archive <ZipFile> -DestinationPath <String>

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

No comments:

Post a Comment