Skip to content

Latest commit

 

History

History
158 lines (109 loc) · 4.79 KB

File metadata and controls

158 lines (109 loc) · 4.79 KB

PowerShell 7

How to install PowerShell and create user profiles.

Install PowerShell 7

Be sure to check the options for adding to $PATH and VS Code.

Disable Telemetry

You can skip this and add it to a profile later.

$env:POWERSHELL_TELEMETRY_OPTOUT

Set Execution Policy

This is a one-time step that cannot be done via a profile. In PS as admin:

$value = Get-ExecutionPolicy
if ($value -eq 'RemoteSigned') {
  Write-Host "Execution policy is currently 'RemoteSigned'. No changes made."
} else {
  Set-ExecutionPolicy RemoteSigned
  Write-Host "Execution policy changed to 'RemoteSigned'."
}

About PowerShell Profiles

At some point you'll want or need to create profiles. I found it best to use 2 profiles - 1 for all users and one for me as the current user.

To create and open profiles, from PowerShell as admin:

# All users, all hosts
Code $profile.AllUsersAllHosts
notepad $profile.AllUsersAllHosts

# Current user, all hosts
Code $Home\Documents\PowerShell\Profile.ps1
notepad $Home\Documents\PowerShell\Profile.ps1

All Users, All Hosts Profile

  • The variable for this file is: $PSHOME\Profile.ps1.
  • The command to locate this file is: $profile.AllUsersAllHosts.

In PowerShell as an admin, test if this file exists and create if needed:

if (Test-Path $profile.AllUsersAllHosts) {
  Write-Host "Profile exists."
} else {
  New-Item -ItemType File -Path $profile.AllUsersAllHosts -Force
  Write-Host "Profile created."  
}

Use this profile to share settings - like starting the OpenSSH server - across users. For example:

# Open profile
Code $profile.AllUsersAllHosts

# Variables
$env:POWERSHELL_TELEMETRY_OPTOUT = 1
$VerbosePreference = "SilentlyContinue"

# Command History
Set-PSReadlineOption -HistoryNoDuplicates

# Oh My Posh
oh-my-posh init pwsh --config ~/.default.omp.json | Invoke-Expression

Current User, All Hosts Profile

  • The variable for this profile is: $PROFILE.CurrentUserAllHosts.
  • The command to locate this file is:$Home\Documents\PowerShell\Profile.ps1

In PowerShell test if this file exists and create if needed:

if (Test-Path $Home\Documents\PowerShell\Profile.ps1) {
  Write-Host "Profile exists."
} else {
  New-Item -ItemType File -Path $Home\Documents\PowerShell\Profile.ps1 -Force
  Write-Host "Profile created."  
}

Use this profile for user-specific settings like SSH to remote servers.

# Open profile 
Code $Home\Documents\PowerShell\Profile.ps1

# wce.red
function wcdred{ssh -i ~/.ssh/wcd-red.pem ubuntu@wcd.red}

# SSH to NAS
function NAS {ssh wcdogg@192.168.1.209 -p7022}

# SSH to wcdUbuntu
function wcdub {ssh -i ~/.ssh/wcd-ubuntu.pem ubuntu@23.20.11.194}

PowerShell and VS Code

You can set PowerShell 7 as the default terminal for VS Code.

# Check and note the installation path
Get-Command pwsh | Select-Object -ExpandProperty Path

C:\Program Files\PowerShell\7\pwsh.exe

In VS Code:

  1. Ctrl + , to open 'Settings'
  2. Search for: terminal.integrated.defaultProfile
  3. On either or both of the 'User' and 'Workspace' tabs, locate the 'Terminal' dropdown and select 'PowerShell'.

Alternatively, you can add this to your settings.json:

"terminal.integrated.defaultProfile.windows": "PowerShell 7",
"terminal.integrated.profiles.windows": {
  "PowerShell 7": {
    "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
  }
}

References