Skip to content

Instantly share code, notes, and snippets.

@saiwolf
Forked from timsneath/profile.ps1
Last active January 29, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saiwolf/b94e102742a36339890ce045c78a69a7 to your computer and use it in GitHub Desktop.
Save saiwolf/b94e102742a36339890ce045c78a69a7 to your computer and use it in GitHub Desktop.
PowerShell template profile: adds some useful aliases and functions
### PowerShell template profile
### Modified By: Robert Cato <saiwolf@swmnu.net>
### Forked From, and All Credit Due:
### PowerShell template profile
### Version 1.03 - Tim Sneath <tim@sneath.org>
### From https://gist.github.com/timsneath/19867b12eee7fd5af2ba
###
### This file should be stored in $PROFILE.CurrentUserAllHosts
### If $PROFILE.CurrentUserAllHosts doesn't exist, you can make one with the following:
### PS> New-Item $PROFILE.CurrentUserAllHosts -ItemType File -Force
### This will create the file and the containing subdirectory if it doesn't already
###
### As a reminder, to enable unsigned script execution of local scripts on client Windows,
### you need to run this line (or similar) from an elevated PowerShell prompt:
### Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
### This is the default policy on Windows Server 2012 R2 and above for server Windows. For
### more information about execution policies, run Get-Help about_Execution_Policies.
# Find out if the current user identity is elevated (has admin rights)
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal $identity
$isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
# Set inital prompt colors
function ConsoleDefaults {
$host.UI.RawUI.BackgroundColor = "Black"
$host.UI.RawUI.ForegroundColor = "Gray"
Clear-Host
}
ConsoleDefaults
# Useful shortcuts for traversing directories
function cd... { cd ..\.. }
function cd.... { cd ..\..\.. }
# Compute file hashes - useful for checking successful downloads.
# Named after traditional *NIX Coreutils programs.
function md5sum { Get-FileHash -Algorithm MD5 $args }
function sha1sum { Get-FileHash -Algorithm SHA1 $args }
function sha256sum { Get-FileHash -Algorithm SHA256 $args }
# Quick shortcut to start notepad (Or Notepad++ if replacing notepad)
function n { notepad $args }
# Explicitly start Notepad++ if installed
function n++ {
# Change this to your Notepad++ install path.
$notepadplusplus = 'C:\Program Files (x86)\Notepad++'
if ([System.IO.Directory]::Exists($notepadplusplus))
{
# Avoid a null-argument error for -ArgumentList
if ($args.Count -gt 0)
{
Start-Process -FilePath "$notepadplusplus\notepad++.exe" -ArgumentList $args
}
else
{
Start-Process -FilePath "$notepadplusplus\notepad++.exe"
}
}
Remove-Variable -Name notepadplusplus
}
# Drive Shortcuts
function HKLM: { Set-Location HKLM: }
function HKCU: { Set-Location HKCU: }
function Env: { Set-Location Env: }
# Creates drive shortcut for Work Folders, if current user account is using it
if (Test-Path "$env:USERPROFILE\Work Folders")
{
New-PSDrive -Name Work -PSProvider FileSystem -Root "$env:USERPROFILE\Work Folders" -Description "Work Folders"
function Work: { Set-Location Work: }
}
# Creates drive shortcut for OneDrive, if current user account is using it
if (Test-Path HKCU:\SOFTWARE\Microsoft\OneDrive)
{
$onedrive = Get-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\OneDrive
if (Test-Path $onedrive.UserFolder)
{
New-PSDrive -Name OneDrive -PSProvider FileSystem -Root $onedrive.UserFolder -Description "OneDrive"
function OneDrive: { Set-Location OneDrive: }
}
Remove-Variable onedrive
}
# Set up command prompt and window title. Use UNIX-style convention for identifying
# whether user is elevated (root) or not. Window title shows current version of PowerShell
# and appends [ADMIN] if appropriate for easy taskbar identification. The `n inserts a newline
# just in case an earlier script or module has output before the prompt is rendered.
#
# Example Output:
# [C:\Windows\System32]
# PS user@computer $> _
function prompt
{
$topString = "`n[" + (Get-Location) + "]"
$bottomString = "PS " + $env:USERNAME + "@" + "$env:COMPUTERNAME"
if (($host.Name -match "ConsoleHost") -and ($isAdmin))
{
$bottomString += " #>"
Write-Host $topString -ForegroundColor Red
Write-Host $bottomString -NoNewline -ForegroundColor Red
return " "
}
if ($isAdmin)
{
$bottomString += " #>"
Write-Host $topString -ForegroundColor Red
Write-Host $bottomString -NoNewline -ForegroundColor Red
return " "
}
else
{
$bottomString += " $>"
Write-Host $topString -ForegroundColor Green
Write-Host $bottomString -NoNewline -ForegroundColor Green
return " "
}
Remove-Variable -Name topString
Remove-Variable -Name bottomString
}
$Host.UI.RawUI.WindowTitle = "PowerShell {0}" -f $PSVersionTable.PSVersion.ToString()
if ($isAdmin)
{
$Host.UI.RawUI.WindowTitle += " [ADMIN]"
}
# Does the the rough equivalent of dir /s /b. For example, dirs *.png is dir /s /b *.png
function dirs
{
if ($args.Count -gt 0)
{
Get-ChildItem -Recurse -Include "$args" | Foreach-Object FullName
}
else
{
Get-ChildItem -Recurse | Foreach-Object FullName
}
}
# Simple function to start a new elevated process. If arguments are supplied then
# a single command is started with admin rights; if not then a new admin instance
# of PowerShell is started.
function admin
{
if ($args.Count -gt 0)
{
$argList = "& '" + $args + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $argList
}
else
{
Start-Process "$psHome\powershell.exe" -Verb runAs
}
}
# Set UNIX-like aliases for the admin command, so sudo <command> will run the command
# with elevated rights.
Set-Alias -Name su -Value admin
Set-Alias -Name sudo -Value admin
# Make it easy to edit this profile once it's installed
function Edit-Profile
{
if ($host.Name -match "ise")
{
$psISE.CurrentPowerShellTab.Files.Add($profile.CurrentUserAllHosts)
}
else
{
notepad $profile.CurrentUserAllHosts
}
}
# We don't need these any more; they were just temporary variables to get to $isAdmin.
# Delete them to prevent cluttering up the user profile.
Remove-Variable identity
Remove-Variable principal
@saiwolf
Copy link
Author

saiwolf commented Jan 29, 2019

I had to use [System.IO.Directory]::Exists() instead of Test-Path for the Notepad++ function.
For some reason, Test-Path always returns false; even when the path is hard-coded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment