Name: DirectoryLoopUploadToAzureStorage.ps1 
Author: Dan Stolts – dstolts&&microsoft.com - http://ITProGuru.com 
Purpose: Demonstrate how upload an entire filtered directory of files to an Azure Storage Account using PowerShell.  
         Obtain the storage key, and upload an entire directory of files to a particular folder using PowerShell 
         Also demonstrates skipping some files based on file extension 
   See Also: Download From Public URL https://gallery.technet.microsoft.com/Downloading-Files-from-dcaaf44c 
   See also: Create Azure Storage https://gallery.technet.microsoft.com/Create-Azure-Storage-f6943b67 
   See Also: Working with Azure Storage: https://gallery.technet.microsoft.com/Working-with-Azure-Storage-6d09ef2b 
NOTE: To connect to Azure, See  
   Accounts:    https://gallery.technet.microsoft.com/Azure-Accounts-with-c1cc7763 
   Subscriptions: https://gallery.technet.microsoft.com/Azure-Subscriptions-with-be76827a 
  Syntax/Execution: Copy portion of script you want to use and paste into PowerShell (or ISE)  
  Disclaimer: Use at your own Risk!  See details at http://ITProGuru.com/privacy  
    * Must Run PowerShell (or ISE)  
    * UAC may get in the way depending on your settings (See: http://ITProGuru.com/PS-UAC) 
    * Requires PowerShell Azure Module (see http://itproguru.com/AzurePowerShell) 
 video of Script will be uploaded to https://channel9.msdn.com/Series/GuruPowerShell 
 More scripts from Dan Stolts "ITProGuru" at http://ITProGuru.com/Scripts 
See Also: Download From Public URL https://gallery.technet.microsoft.com/Downloading-Files-from-dcaaf44c 
$mySubName = "Internal Consumption" 
$StorageName = "tr21gurusql14"  #Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only 
$ContainerName = "files"        # Folder in Azure must already exist. To create, see https://gallery.technet.microsoft.com/Create-Azure-Storage-f6943b67  
# Open Storage  
$StorageKey = Get-AzureStorageKey -StorageAccountName $StorageName | %{ $_.Primary }  # Get the primary Key 
$StorageContext = New-AzureStorageContext -StorageAccountKey $StorageKey -StorageAccountName $StorageName # Get the Context 
#$StorageAccount = Get-AzureStorageAccount -StorageAccountName $StorageName  # Get the Storage Account 
Set-AzureSubscription -SubscriptionName $mySubName -CurrentStorageAccountName $StorageName 
cd \tr21\temp     # Directory with files to upload must already exist.  
$dir = Get-Location 
Write-Host "Uploading Files to Container... " $StorageContext.BlobEndPoint $ContainerName  -ForegroundColor Green  
# $_.mode -match "-a---" scans the data directory and only fetches the files. It filters out all directories 
$files = Get-ChildItem $dir -force| Where-Object {$_.mode -match "-a---"# Filter for better fp 
# you could also use the -filter "*.ps1" as a parameter to Get-ChildItem to only grab certain files. 
#    Example: Get-ChildItem C:\Scripts, -filter "*.ps1" 
# you can also look at multiple folders at the same time  
#    Example: Get-ChildItem C:\Scripts, C:\Test, "C:\Documents and Settings\ITProGuru\Desktop" -filter "*.ps1" 
# iterate through all the files and start uploading data 
foreach ($file in $files){ 
   #fqName represents fully qualified name 
   $fqName = $file.FullName 
   # Examples of working with other properties... 
            # $file.DirectoryName ; $file.Mode ; $file.Attributes ; $file.Directory 
            # $file.Exists ; $file.Extension ;  $file.Name ; $file.VersionInfo 
            # Note: simi-colon to run multiple commands on the same line :) 
   if ($file.Extension -ne ".publishsettings") {     # Exclude PublishSettings Files 
      Write-Host "Uploading " $dir $file.Name  -ForegroundColor Green  
      Set-AzureStorageBlobContent -Blob $file.Name -Container $ContainerName -File $fqName -Context $StoreContext -Force 
# video of Script will be uploaded to https://channel9.msdn.com/Series/GuruPowerShell 
# More scripts from Dan Stolts "ITProGuru" at http://ITProGuru.com/Scripts 
 Demonstrate how upload an entire filtered directory of files to an Azure Storage Account using PowerShell.
         Obtain the storage key, and upload an entire directory of files to a particular folder using PowerShell
         Also demonstrates skipping some files based on file extension
   See Also: Download From Public URL https://gallery.technet.microsoft.com/Downloading-Files-from-dcaaf44c
   See also: Create Azure Storage https://gallery.technet.microsoft.com/Create-Azure-Storage-f6943b67
   See Also: Working with Azure Storage: https://gallery.technet.microsoft.com/Working-with-Azure-Storage-6d09ef2b
NOTE: To connect to Azure, See
   Accounts:    https://gallery.technet.microsoft.com/Azure-Accounts-with-c1cc7763
   Subscriptions: https://gallery.technet.microsoft.com/Azure-Subscriptions-with-be76827a

Brought to you by ITProGuru Dan Stolts. (Microsoft –Chief Technology Strategist)

Blog: http://ITProGuru.com

Script Home: http://ITProGuru.com/Scripts

Accompanying Video soon on Channel 9 at https://channel9.msdn.com/Series/GuruPowerShell


Special ASK: Please use the Copy Button to Copy the code
Please Rate this article.  If it was helpful, flag 4 or 5 stars which are the positive values