PS: Testing And Setting PS Profiles

3 minute read

Description:

So as you may or may not know, PS (powershell) uses six profiles when launching scripts and hey!, even VS Code has one, so it’s a hassle trying to keep them all synced up. These are scripts I use to help keep them in check on different machines.

To Resolve:

  1. This script will test to see if they exists and if not, create them:
If (Test-Path $profile)
{
Log "Powershell CurrentUser,CurrentHost Profile Already exists" 
}
Else
{
New-Item $profile -ItemType file -Force
Log "Created Powershell CurrentUser,CurrentHost Profile" 
}

If (Test-Path $profile.CurrentUserAllHosts)
{
Log "Powershell CurrentUser,AllHost Profile already exists" 
}
Else
{
New-Item $profile.CurrentUserAllHosts -ItemType file -Force
Log "Created Powershell CurrentUser,AllHost profile" 
}

If (Test-Path $profile.AllUsersCurrentHost)
{
Log "Powershell AllUsers,CurrentHost profile already exists" 
}
Else
{
New-Item $profile.AllUsersCurrentHost -ItemType file -Force
Log "Created Powershell AllUsers,CurrentHost profile" 
}

If (Test-Path $profile.AllUsersAllHosts)
{
Log "Powershell AllUsers,AllHosts profile already exists" 
}
Else
{
New-Item $profile.AllUsersAllHosts -ItemType file -Force
Log "Created Powershell AllUsers,AllHosts profile" 
}

# This is for Powershell ISE

$isePath = "$env:userprofile\Documents\WindowsPowershell\Microsoft.PowerShellISE_profile.ps1"
If (Test-Path $isePath)
{
Log "Powershell ISE CurrentUser, CurrentHost profile already exists" 
}
Else
{
New-Item -path $isePath -ItemType file -Force
Log "Created Powershell ISE CurrentUser, CurrentHost profile" 
}

$iseAllUsersPath = "$env:userprofile\Documents\Microsoft.PowerShellISE_profile.ps1"
If (Test-Path $iseAllUsersPath)
{
Log "Powershell ISE AllUsers, CurrentHost profile already exists" 
}
Else
{
New-Item -path $iseAllUsersPath -ItemType file -Force
Log "Created Powershell ISE AllUsers, CurrentHost profile" 
}
  1. This script will set them to my current Profile’s setup:
$Files = @("$env:USERPROFILE\Documents\WindowsPowershell\Microsoft.Powershell_profile.ps1",
		"$env:USERPROFILE\Documents\WindowsPowershell\Microsoft.PowershellISE_profile.ps1",
		"$env:USERPROFILE\Documents\WindowsPowershell\Microsoft.VSCode_profile.ps1")
	
	ForEach ($F in $Files)
	{
		# Remember to use the escape character "`" before every dollar sign and ` character. For example `$myVar and ``r``n (new line)
		$val = 
		@"
<#######<Script>#######>
<#######<Header>#######>
# Name: PS Profile Script
# Copyright: Gerry Williams (https://gerrywilliams.net)
# License: MIT License (https://opensource.org/licenses/mit)
# Script Modified from: n/a
<#######</Header>#######>
<#######<Body>#######>

# Import Modules
# Import-Module -Name ActiveDirectory, Applications, Configuration, Filesystem, Misc, Networking, Security
Set-Location C:\scripts

Import-Module psColor
`$global:PSColor.File.Executable.Color = 'DarkGreen'

# Preferences

Function Test-IsAdmin
{
<#
	.Synopsis
	Determines whether or not the user is a member of the local Administrators security group.
	.Outputs
	System.Bool
#>
[CmdletBinding()]

`$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
`$Principal = new-object System.Security.Principal.WindowsPrincipal(`${Identity})
`$IsAdmin = `$Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
Write-Output -InputObject `$IsAdmin;
}

Function Set-Console
{
<# 
	.Synopsis
	Function to set console colors just for the session.
	.Description
	Function to set console colors just for the session.
	I mainly did this because darkgreen does not look too good on blue (Powershell defaults).
	.Notes
	2017-10-19: v1.0 Initial script 
	#>
	
`$console = `$host.UI.RawUI
if (Test-IsAdmin)
{
	`$console.WindowTitle = "[!] Powershell: Administrator"
}
Else
{
	`$console.WindowTitle = "[-] Powershell: Unelevated"
}
`$background = "black"
`$Host.UI.RawUI.BackgroundColor = `$background 
`$Host.UI.RawUI.ForegroundColor = "green"
`$Host.PrivateData.ErrorForegroundColor = "darkcyan"
`$Host.PrivateData.ErrorBackgroundColor = `$background 
`$Host.PrivateData.WarningForegroundColor = "darkgray"
`$Host.PrivateData.WarningBackgroundColor = `$background 
`$Host.PrivateData.DebugForegroundColor = "darkblue"
`$Host.PrivateData.DebugBackgroundColor = `$background 
`$Host.PrivateData.VerboseForegroundColor = "yellow"
`$Host.PrivateData.VerboseBackgroundColor = `$background 
`$Host.PrivateData.ProgressForegroundColor = "darkmagenta"
`$Host.PrivateData.ProgressBackgroundColor = `$background 
Clear-Host
}
Set-Console

Function Get-Help2
{
<# 
.Synopsis
Launches a window in SS64.com for a selected command.
.Description
Launches a window in SS64.com for a selected command.
.Example
Get-Help Get-Process
Opens a window in your default internet browser to ss64's page on get-process.
.Notes
2017-10-19: v1.0 Initial script 
#>
param
(
	[string]`$command
)
`$command =`$command.ToLower()
Start-process -filepath "https://ss64.com/ps/`$command.html"
}

Function Prompt
{
<# 
.Synopsis
Function to set the prompt to the date and directory on one line and just a pound sign on the second line. 
If you are running as admin, it will also put an [Admin] in front on the first line.
.Description
Function to set the prompt to the date and directory on one line and just a pound sign on the second line. 
If you are running as admin, it will also put an [Admin] in front on the first line.
.Notes
2017-10-26: v1.0 Initial script 
#>

`$Identity = [Security.Principal.Windowsidentity]::Getcurrent()
`$Principal = [Security.Principal.Windowsprincipal] `$Identity
`$(If (Test-Path Variable:/Psdebugcontext)
{ 
	'[Dbg]: ' 
}

Elseif (`$Principal.Isinrole([Security.Principal.Windowsbuiltinrole] "Administrator"))
{
	Write-Host ("[Admin] ") -Nonewline
	Write-Host ("[") -Nonewline
	Write-Host (Get-Date -Format 'G') -Nonewline
	Write-Host ("][") -Nonewline
	Write-Host (`$Pwd) -Nonewline
	Write-Host ("]") -Nonewline
	Write-Host ("`r`n") -Nonewline
	Write-Host ("#") -Nonewline
	Return " ";
}

Else
{
	Write-Host ("[") -Nonewline
	Write-Host (Get-Date -Format 'G') -Nonewline
	Write-Host ("][") -Nonewline
	Write-Host (`$Pwd) -Nonewline
	Write-Host ("]") -Nonewline
	Write-Host ("`r`n") -Nonewline
	Write-Host ("#") -Nonewline
	Return " ";
})
}

<#######</Body>#######>
<#######</Script>#######>
"@
		Set-Content -Path $F -Value $val
		Log "Profile $F has been set"
	}

This can be found in my gwConfiguration section.