You are here: cyanghost > PowerShell Notes

PowerShell Notes

This page is still a work in progress.

Here are some commands that have been useful to me when using PowerShell both at work and at home. They all mostly have to do with Azure. In order for most of these to work, you'll need to download and install Microsoft Azure PowerShell.

Where you see the <> characters, those are fields you have to replace according to your set up.

Command to associate your Azure account with PowerShell: Add-AzureAccount

Sometimes, it will spit back an error. The first step I try is to run this command: Clear-AzureProfile -Force
In my experience, that command solves it most of the time.

Getting Azure VPN Tunnel PSKs:

Get-AzureVNetGatewayKey -VNetName "VNetName" -LocalNetworkSiteName "Site Name"

Where it says VNetName, you'll need to put the name of the Virtual Network you assigned in Azure. Replace Site Name with the name of the VPN site. Sometimes, Azure adds some weird characters to those, so it may not be what it is verbatim in Azure. To find the actual site name, you'll need to run: Get-AzureVNetConfig -ExportToFile "$home\Desktop\GatewaySites.txt"

I have it set to export a file titled "GatewaySites.txt" to the desktop because if I just run the command without that piece, a large part of the command is truncated so the site names will display in that text file. I'm sure there's a way to set it not to truncate information but I haven't found it yet.

Set static IPs on Servers

In order to assign static IPs to virtual machines in Azure, you'll need to stop them first:

Get-AzureVM -ServiceName <YourService> | Stop-AzureVM -Force

Once you've done that, we can assign IPs to our servers.

Get-AzureVM -ServiceName <YourService> -Name <ServerName>

Set-AzureStaticVNetIP -IPAddress <>


Once you've done that for all your severs, you can spin them all back up with this one command:

Get-AzureVM -ServiceName <YourService> | Start-AzureVM -Force

I'm going to assume that you're doing this during initial deployment stages, but in case you're not, it goes without saying that you should do this after business hours.

Migrate User Files to New Location

This might be helpful in cases of simply moving a lot of files from multiple locations to another at once. I found this particularly helpful when migrating off domain machines to the domain which uses Folder Redirection.

I'm going to move files from the user's current Desktop, Documents and Downloads over to the new server. First, I'll go ahead and define my variables. I'm assuming that you're already logged into the domain account.

$domainUsername: [Environment]::UserName
$localUsername: <jdoe>
$localUserProfilePath = "C:\Users\$localUsername"
$domainUserProfilePath = "\\<Server>\<Users>\$domainUsername"

Now, I will tell it to start copying the files over to the server.

Copy-Item -Recurse -Path "$localUserProfilePath\Desktop\*" -Destination "$domainUserProfilePath\Desktop\"

Copy-Item -Recurse -Path "$localUserProfilePath\Documents\*" -Destination "$domainUserProfilePath\Documents\"

Copy-Item -Recurse -Path "$localUserProfilePath\Downloads\*" -Destination "$domainUserProfilePath\Downloads\"

PowerShell Mailer

This would be useful if you had a script that needed to send email notifications via an SMTP server. My only concern with this one is that the password for the email account can be stored in plaintext. I recommend that you hash the password, but for demonstration purposes I'll put an example here.

$EmailTo = ""
$EmailFrom = ""
$Subject = "Subject goes here."
$Body = "Message goes here."
$SMTPServer = ""
$filenameAndPath = "C:\files\attachment.txt" (used to add an attachment)
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) (most SMTP servers use port 587, but you can change it if needed)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<", "<Hashed password goes here.");