Azure Azure Migrate PowerShell

PowerShell script: AzCopy (Windows 64-bit) download and silent installation


AzCopy is a free command-line tool that is offered by Microsoft. It allows you to easily copy and transfer data (data migration) from and to Azure storage. It is designed for high performance transfers and can be deployed on both Windows (32-bit and 64-bit) and Linux systems, but also on macOS.

AzCopy for example allows users to copy data (blobs or files) between a file system and an Azure storage account, or between storage accounts. Users have the possibility to select items by specifying patterns, like wildcards or prefixes, to identify the needed files for upload or download. It currently supports Microsoft Azure Blob, File and Table storage.

Important to keep in mind is that, if you need to be able to copy data to and from Azure Table storage, you need to use AzCopy version 7.3

To automate the download and silent installation process of this useful tool, I wrote the below PowerShell script which does all of the following:


  • Check if the PowerShell window is running as Administrator (which is a requirement), otherwise the PowerShell window will be closed.
  • Create the _Admin_Tools folder on the C: drive if the folder not already exists.
  • Delete the AzCopy folder and all files in the folder, if the folder already exists in the _Admin_Tools folder.
  • Download the latest AzCopy (Windows 64-bit) .zip file and save under the _Admin_Tools folder.
  • Extract the .zip file into the _Admin_Tools folder.
  • Delete the .zip file after extraction.
  • Rename the azcopy_windows_*.* folder to AzCopy.
  • Add the AzCopy folder, which holds the executable (.exe), to the PATH system variable for ease of use.

To use the script copy and save it as AzCopy_Download_and_Silent_Installation.ps1 or download it from GitHub. First adjust all variables to your use and afterwards run the script with Administrator privileges from Windows Terminal, Windows PowerShell, or Visual Studio Code.

You can also find more information about AzCopy, on Microsoft Docs.

PowerShell script

<#
.SYNOPSIS

A script used to download and silently install the latest AzCopy Windows 64-bit executable (.exe) file into the C:\_Admin_Tools\AzCopy folder.

.DESCRIPTION

The latest AzCopy Windows 64-bit executable (.exe) file will be downloaded from a static download link, extracted, and made availabele into the C:\_Admin_Tools\AzCopy folder.
The directory location of the AzCopy executable will also be added to thye system path for ease of use.

.NOTES

Filename:       AzCopy_Download_and_Silent_Installation.ps1
Created:        02/03/21
Last modified:  02/03/21
PowerShell:     5.1
Version:        1.0
Author:         Wim Matthyssen
Twitter:        @wmatthyssen
Action:         Change variables were needed to fit your needs and run as Administrator
Disclaimer:     This script is provided "As IS" with no warranties.

.EXAMPLE

.\AzCopy_Download_and_Silent_Installation.ps1

.LINK

https://tinyurl.com/2x6xxdr3
#>
 
## Variables

$scriptName = "AzCopy_Download_and_Silent_Installation"
$adminToolsFolderName = "_Admin_Tools"
$adminToolsFolder = "C:\" + $adminToolsFolderName +"\"
$itemType = "Directory"
$azCopyFolderName = "AzCopy"
$azCopyFolder = $adminToolsFolder + $azCopyFolderName 
$azCopyUrl = (curl https://aka.ms/downloadazcopy-v10-windows -MaximumRedirection 0 -ErrorAction silentlycontinue).headers.location
$azCopyZip = "azcopy.zip"
$azCopyZipLocation = $adminToolsFolder + $azCopyZip

$writeEmptyLine = "`n"
$writeSeperator = " - "
$writeSpace = " "
$global:currentTime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currentTime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$foregroundColor2 = "Yellow"

##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Check if running as Administrator, otherwise close the PowerShell window

$CurrentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$IsAdministrator = $CurrentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($IsAdministrator -eq $false) {
    Write-Host ($writeEmptyLine + "# Please run PowerShell as Administrator" + $writeSeperator + $currentTime)`
    -foregroundcolor $foregroundColor1 $writeEmptyLine
    Start-Sleep -s 5
    exit
}
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Start script execution

Write-Host ($writeEmptyLine + "#" + $writeSpace + $scriptName + $writeSpace + "Script started" + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine 
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Create C:\_Admin_Tools folder if not exists

If(!(test-path $adminToolsFolder))
{
New-Item -Path "C:\" -Name $adminToolsFolderName -ItemType $itemType -Force | Out-Null
}

Write-Host ($writeEmptyLine + "#" + $writeSpace + $adminToolsFolderName + $writeSpace + "folder available" + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Delete AzCopy folder if already exists in _Admin_Tools folder

If(test-path $azCopyFolder)
{
Remove-Item $azCopyFolder -Recurse | Out-Null
}

Write-Host ($writeEmptyLine + "#" + $writeSpace + $azCopyFolderName + $writeSpace + "folder not available" + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Write Download started

Write-Host ($writeEmptyLine + "#" + $writeSpace + $azCopyFolderName + $writeSpace + "download started" + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine 
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Download, extract and cleanup the latest AzCopy zip file
 
Invoke-WebRequest $azCopyUrl -OutFile $azCopyZipLocation
Expand-Archive -LiteralPath $azCopyZipLocation -DestinationPath $adminToolsFolder -Force
Remove-Item $azCopyZipLocation

Write-Host ($writeEmptyLine + "#" + $writeSpace + "azcopy.exe available" + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine 
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Rename AzCopy folder

$azCopyOriginalFolderName = Get-ChildItem -Path $adminToolsFolder -Name azcopy*
$azCopyFolderToRename = $adminToolsFolder + $azCopyOriginalFolderName
$azCopyFolderToRenameTo = $adminToolsFolder + $azCopyFolderName

Rename-Item $azCopyFolderToRename $azCopyFolderToRenameTo

Write-Host ($writeEmptyLine + "#" + $writeSpace + "azcopy folder renamed to" + $writeSpace + $azCopyFolderName + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine 
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Add the AzCopy folder to the PATH System variable

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$azCopyFolderToRenameTo", "Machine")

Write-Host ($writeEmptyLine + "#" + $writeSpace + "The directory location of the AzCopy executable is added to the system path" + $writeSpace `
+ $writeSeperator + $currentTime) -foregroundcolor $foregroundColor2 $writeEmptyLine 
 
##-------------------------------------------------------------------------------------------------------------------------------------------------------

## Write script completed

Write-Host ($writeEmptyLine + "#" + $writeSpace + "Script completed" + $writeSeperator + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine

##-------------------------------------------------------------------------------------------------------------------------------------------------------


I hope this PowerShell script is useful for you and provides you with a good starting point to use AzCopy. If you have any questions or recommendations about it, feel free to contact me through my Twitter handle or to leave a comment.

0 comments on “PowerShell script: AzCopy (Windows 64-bit) download and silent installation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: