Skip to content

Instantly share code, notes, and snippets.

@aggieben
Last active July 14, 2020 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aggieben/6c5d5c6c7985b506f35030797cd10392 to your computer and use it in GitHub Desktop.
Save aggieben/6c5d5c6c7985b506f35030797cd10392 to your computer and use it in GitHub Desktop.
PowerShell Profile Scripts

Overview

These files are part of how I build a powershell profile.

This is what it looks like at startup:

Terminal Startup

Setup Instructions

Prerequisites

You need the following PS Modules installed before you start:

  • pscx
  • PSReadline
  • posh-vs

All of these can be installed with Install-Module.

You also need to have starship in your path somewhere. You can install this via cargo (the Rust language package manager) as I have done or via Chocolatey (or by whatever other means you like if you're informed about other ways).

Profile Setup

Download this gist as a ZIP. Copy 01_PowerShell_common.ps1 to $profile. Copy the *.xml files to $PSScriptRoot. Create one or more local customizations in $PSScriptRoot\{a label of your choosing}.local.ps1.

PowerShell Core

These instructions should work for PowerShell Core as well; just be aware that PowerShell and PowerShell Core have separate profile directories.

$PROFILE_TIMING_ON=$True
Function Profile-Command($label, $block) {
if ($block -isnot [ScriptBlock]) {
throw "expected ScriptBlock, got $($block.GetType())"
}
Measure-Command $block `
| ForEach-Object {
if ($PROFILE_TIMING_ON) {
Write-Host "$_ [$label]"
}
}
}
Function Import-VisualStudio {
Import-BatchEnvironment "$(vswhere -latest -property installationPath)\Common7\Tools\VsDevCmd.bat"
}
Profile-Command "Imports" {
Import-Module "pscx"
Import-Module "PSReadline"
Import-Module "posh-vs"
}
Profile-Command "Configure PSReadline" {
Set-PSReadlineOption -EditMode Emacs
}
Profile-Command "Theme" {
Set-Theme Paradox
}
Profile-Command "Custom Type Formatters" {
Update-TypeData -PrependPath $PSScriptRoot\MyTypes.ps1xml
Update-FormatData -PrependPath $PSScriptRoot\MyFileFormat.format.ps1xml
}
Profile-Command "Starship" {
Invoke-Expression (&starship init powershell)
}
Profile-Command "Loading Customized Profile Scripts" {
Get-ChildItem $PSScriptRoot -filter "*.local.ps1" |% { . $_.FullName }
}
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<!-- Filesize converts the length to a human readable
format (kb, mb, gb, tb) -->
<Name>FileSize</Name>
<GetScriptBlock>
switch($this.length) {
{ $_ -gt 1tb }
{ "{0:n2} TB" -f ($_ / 1tb) ; break }
{ $_ -gt 1gb }
{ "{0:n2} GB" -f ($_ / 1gb) ; break }
{ $_ -gt 1mb }
{ "{0:n2} MB " -f ($_ / 1mb) ; break }
{ $_ -gt 1kb }
{ "{0:n2} KB " -f ($_ / 1Kb) ; break }
default
{ "{0} B " -f $_}
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<SelectionSets>
<SelectionSet>
<Name>FileSystemTypes</Name>
<Types>
<TypeName>System.IO.DirectoryInfo</TypeName>
<TypeName>System.IO.FileInfo</TypeName>
</Types>
</SelectionSet>
</SelectionSets>
<!-- ################ GLOBAL CONTROL DEFINITIONS ################ -->
<Controls>
<Control>
<Name>FileSystemTypes-GroupingFormat</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<Text AssemblyName="System.Management.Automation" BaseName="FileSystemProviderStrings" ResourceId="DirectoryDisplayGrouping"/>
<ExpressionBinding>
<ScriptBlock>
$_.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "")
</ScriptBlock>
</ExpressionBinding>
<NewLine/>
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
<!-- ################ VIEW DEFINITIONS ################ -->
<ViewDefinitions>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Mode</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>LastWriteTime</Label>
<Width>25</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>FileSize</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader/>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap/>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
[String]::Format("{0,10} {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>FileSize</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<ListControl>
<ListEntries>
<ListEntry>
<EntrySelectedBy>
<TypeName>System.IO.FileInfo</TypeName>
</EntrySelectedBy>
<ListItems>
<ListItem>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<PropertyName>FileSize</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CreationTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastWriteTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastAccessTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Mode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LinkType</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Target</PropertyName>
</ListItem>
<ListItem>
<PropertyName>VersionInfo</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CreationTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastWriteTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastAccessTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Mode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LinkType</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Target</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<WideControl>
<WideEntries>
<WideEntry>
<WideItem>
<PropertyName>Name</PropertyName>
</WideItem>
</WideEntry>
<WideEntry>
<EntrySelectedBy>
<TypeName>System.IO.DirectoryInfo</TypeName>
</EntrySelectedBy>
<WideItem>
<PropertyName>Name</PropertyName>
<FormatString>[{0}]</FormatString>
</WideItem>
</WideEntry>
</WideEntries>
</WideControl>
</View>
<View>
<Name>FileSecurityTable</Name>
<ViewSelectedBy>
<TypeName>System.Security.AccessControl.FileSystemSecurity</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Path</Label>
</TableColumnHeader>
<TableColumnHeader />
<TableColumnHeader>
<Label>Access</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>
split-path $_.Path -leaf
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Owner</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$_.AccessToString
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>FileSystemStream</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.Commands.AlternateStreamData</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>Filename</PropertyName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>20</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Width>10</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Stream</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Length</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment