Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created July 15, 2015 13:17
Show Gist options
  • Save wpsmith/b406c64b07c0dbf25258 to your computer and use it in GitHub Desktop.
Save wpsmith/b406c64b07c0dbf25258 to your computer and use it in GitHub Desktop.
PowerShell: Profile to always launch PS as Administrator, set Aliases (ex. Notepad++), Import SharePoint Online, Azure AD, and Azure Services.
# Check if Running as Admin
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $IsAdmin)
{
if ($MyInvocation.ScriptName -ne "")
{
try
{
Write-Host "Relanuching Script as Admin"
$arg = "-file `"$($MyInvocation.ScriptName)`""
Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'
}
catch
{
Write-Warning "Error - Failed to restart script with runas"
break
}
}
else
{
try
{
Write-Host "Relanuching Shell as Admin"
Start-Process "$psHome\powershell.exe" -Verb Runas -ErrorAction 'stop'
}
catch
{
Write-Warning "Error - Failed to restart script with runas"
break
}
}
Write-Host "Exiting"
Stop-Process -Id $PID
}
# Shell Modifications
$Shell = $Host.UI.RawUI
# Set Window Size
$size = $Shell.WindowSize
$size.width=100
$size.height=25
$Shell.WindowSize = $size
# Set BufferSize
$size = $Shell.BufferSize
$size.width=100
$size.height=5000
$Shell.BufferSize = $size
# Add Aliases
new-item alias:np -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
# Clear the Screen
Clear-Host
# Import SPO
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
# Import Azure AD
Import-Module MSOnline
#Import
Set-ExecutionPolicy Bypass
cd "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services"
.\ShortcutStartup.ps1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment