├── Admin_Git.png ├── LICENSE ├── Non-Admin.png ├── README.md ├── Themes └── TsekNet.psm1 ├── install.ps1 └── profile.ps1 /Admin_Git.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TsekNet/PowerShell-Profile/b085e30ea492e5c9c20ceabef5435ea85f0b7f1b/Admin_Git.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Non-Admin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TsekNet/PowerShell-Profile/b085e30ea492e5c9c20ceabef5435ea85f0b7f1b/Non-Admin.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # My PowerShell Profile 2 | 3 | 🚨 This repository is no longer maintained. See [github.com/tseknet/dotfiles](https://github.com/tseknet/dotfiles) for my latest PowerShell profile. 4 | 5 | My PowerShell profile is a collection of functions that add neat shortcuts and 6 | functionality to all of my PowerShell consoles by default. 7 | 8 | See this [blog post](https://tseknet.com/blog/psprofile) for a detailed breakdown. 9 | 10 | ## Installation 11 | 12 | If you want to just get started with copying my homework run the following command in 13 | PowerShell as administrator: 14 | 15 | ```powershell 16 | iex ((New-Object Net.WebClient).DownloadString('https://github.com/tseknet/PowerShell-Profile/raw/master/install.ps1')) 17 | ``` 18 | 19 | ⚠ As always, be sure you're aware of what you're downloading from the internet. You can view the source on GitHub and inspect the code prior to 20 | downloading/executing anything described in this post. I've begun working on making my profile into a [PowerShell module](https://github.com/tseknet/tsekprofile) to make this easier to install. 21 | 22 | ## Usage 23 | 24 | The following functions are included with this profile: 25 | 26 | ```powershell 27 | Set-Path, Import-MyModules, Get-Elevation, Set-WindowTitle, Import-GitRepo, Install-Fonts, Copy-LastCommand, Edit-Profile, Open-HistoryFile, Get-FileHash256, Get-ExportedFunctions 28 | ``` 29 | 30 | ## Screenshots 31 | 32 | PowerShell running in an administrative window while working on a git repo 33 | 34 | ![PowerShell Admin](Admin_Git.png) 35 | 36 | PowerShell running in a non-admin window while working in `C:\Tmp` 37 | 38 | ![PowerShell Non-Admin](Non-Admin.png) 39 | 40 | ## Included Modules 41 | 42 | I can't take all the credit. The following (awesome) modules will be installed 43 | by default: 44 | 45 | 1. [posh-git](https://github.com/dahlbyk/posh-git): Integrates Git and 46 | PowerShell by providing Git status summary information 47 | 1. [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh): Theme engine for 48 | PowerShell 49 | 1. [Get-ChildItemColor](https://github.com/joonro/Get-ChildItemColor): Provides 50 | colorization of outputs of `Get-ChildItem` Cmdlet of PowerShell 51 | 1. [PSWriteHTML](https://github.com/EvotecIT/PSWriteHTML): Output PowerShell 52 | commands to a formatted HTML page 53 | 54 | ## Contributing 55 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 56 | 57 | ## License 58 | [MIT](https://choosealicense.com/licenses/mit/) 59 | -------------------------------------------------------------------------------- /Themes/TsekNet.psm1: -------------------------------------------------------------------------------- 1 | #requires -Version 5 -Modules posh-git 2 | 3 | <# 4 | .Synopsis 5 | Custom oh-my-posh theme built by @TsekNet. 6 | 7 | .DESCRIPTION 8 | This module is a custom theme built using the oh-my-posh PowerLine module. 9 | You should be able to just run Set-Theme Beast if you've already got 10 | oh-my-posh installed. If not, you will get errors, but you will be able to 11 | run this module seperately with a few modfiications (mainly removing any 12 | module specific variables.) 13 | 14 | .NOTES 15 | This is considered in progress and if you see any issues, feel free to submit 16 | a pull request. 17 | 18 | .FUNCTIONALITY 19 | This theme can be loaded using oh-my-posh module and running the Set-Theme 20 | Beast command. 21 | #> 22 | 23 | function Get-Elapsed { 24 | <# 25 | .Synopsis 26 | Get the time span elapsed during the execution of command (by default the previous command) 27 | .Description 28 | Calls Get-History to return a single command and returns the difference between the Start and End execution time 29 | #> 30 | [CmdletBinding()] 31 | param( 32 | # The command ID to get the execution time for (defaults to the previous command) 33 | [Parameter()] 34 | [int]$Id, 35 | 36 | # A Timespan format pattern such as "{0:ss\.ffff}" 37 | [Parameter()] 38 | [string]$Format = "{0:h\:mm\:ss\.ffff}" 39 | ) 40 | $null = $PSBoundParameters.Remove("Format") 41 | $LastCommand = Get-History -Count 1 @PSBoundParameters 42 | if (!$LastCommand) { return "0:00:00.0000" } 43 | $Duration = $LastCommand.EndExecutionTime - $LastCommand.StartExecutionTime 44 | $Format -f $Duration 45 | } 46 | 47 | function Write-Theme { 48 | param( 49 | [bool] 50 | $lastCommandFailed, 51 | [string] 52 | $with 53 | ) 54 | 55 | $prompt = Write-Prompt -Object $sl.PromptSymbols.StartSymbol -ForegroundColor $sl.Colors.White -BackgroundColor $sl.Colors.SessionInfoBackgroundColor 56 | 57 | # Check the last command state and indicate if failed 58 | If ($lastCommandFailed) { 59 | $prompt += Write-Prompt -Object $sl.PromptSymbols.WarningSymbol -ForegroundColor $sl.Colors.White -BackgroundColor $sl.Colors.Red 60 | $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $sl.Colors.Red -BackgroundColor $sl.Colors.Cyan 61 | } 62 | 63 | # Add invocation number 64 | $prompt += Write-Prompt -Object " $($MyInvocation.HistoryId) " -ForegroundColor $sl.Colors.DarkGray -BackgroundColor $sl.Colors.Cyan 65 | $prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $sl.Colors.Cyan -BackgroundColor $sl.Colors.Gray 66 | 67 | $fullPath = $pwd.Path.Replace("$($env:SystemDrive)\", '').Replace('\', ' ' + $sl.PromptSymbols.ForwardHollowArrow + ' ') 68 | 69 | if (Test-VirtualEnv) { 70 | $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.Red 71 | $prompt += Write-Prompt -Object "$($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.White -BackgroundColor $sl.Colors.Red 72 | $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.Red -BackgroundColor $sl.Colors.DarkCyan 73 | } 74 | else { 75 | $prompt += Write-Prompt -Object " $env:SystemDrive " -ForegroundColor $sl.Colors.DarkCyan -BackgroundColor $sl.Colors.Gray 76 | $prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $sl.Colors.Gray -BackgroundColor $sl.Colors.DarkCyan 77 | } 78 | 79 | # Writes the drive portion 80 | # Reduce the path displayed if it is long 81 | if (($fullPath.Split($sl.PromptSymbols.ForwardHollowArrow).count -gt 2)) { 82 | $One = $fullPath.split($sl.PromptSymbols.ForwardHollowArrow)[-1] 83 | $Two = $fullPath.split($sl.PromptSymbols.ForwardHollowArrow)[-2] 84 | 85 | if ($One.Length -gt 10) { $One = "$($One[0..7] -join '')~" } 86 | if ($Two.Length -gt 10) { $Two = "$($Two[0..7] -join '')~" } 87 | 88 | $prompt += Write-Prompt -Object " $($fullPath.split($sl.PromptSymbols.ForwardHollowArrow)[0], " .. ", $Two, $One -join ($sl.PromptSymbols.ForwardHollowArrow)) " -ForegroundColor $sl.Colors.White -BackgroundColor $sl.Colors.DarkCyan 89 | } 90 | else { 91 | $prompt += Write-Prompt -Object " $fullPath "-ForegroundColor $sl.Colors.White -BackgroundColor $sl.Colors.DarkCyan 92 | } 93 | 94 | # Adds Pre/Postfix to the VCS Prompt 95 | $status = Get-VCSStatus 96 | if ($status) { 97 | $themeInfo = Get-VcsInfo -status ($status) 98 | $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $sl.Colors.DarkCyan -BackgroundColor $sl.Colors.Magenta 99 | $prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $sl.Colors.Magenta -ForegroundColor $sl.Colors.Black 100 | $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $sl.Colors.Magenta 101 | } 102 | else { 103 | $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $sl.Colors.DarkCyan 104 | } 105 | 106 | $timestamp = Get-Date -f "T" 107 | 108 | # Writes the Invocation time and date 109 | $prompt += Set-CursorForRightBlockWrite -textLength ($timestamp.Length + 13) 110 | $prompt += Write-Prompt $($sl.PromptSymbols.SegmentBackwardSymbol) -ForegroundColor $sl.Colors.DarkCyan 111 | $prompt += Write-Prompt $(Get-Elapsed) -ForegroundColor $sl.Colors.Yellow -BackgroundColor $sl.Colors.DarkCyan 112 | $prompt += Write-Prompt $($sl.PromptSymbols.SegmentBackwardSymbol) -ForegroundColor $sl.Colors.Blue -BackgroundColor $sl.Colors.DarkCyan 113 | $prompt += Write-Prompt $timestamp -ForegroundColor $sl.Colors.Yellow -BackgroundColor $sl.Colors.Blue 114 | 115 | # Move the actual prompt to the next line and set the prompt 116 | $prompt += Set-Newline 117 | 118 | if ($with) { 119 | $prompt += Write-Prompt -Object "$($with.ToUpper()) " -BackgroundColor $sl.Colors.Magenta -ForegroundColor $sl.Colors.DarkRed 120 | } 121 | 122 | $prompt += Write-Prompt -Object " $($sl.PromptSymbols.HeartSymbol) " -ForegroundColor $sl.Colors.Red -BackgroundColor $sl.Colors.Gray 123 | $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.Gray 124 | $prompt += ' ' 125 | $prompt 126 | } 127 | 128 | $sl = $global:ThemeSettings #local settings 129 | $sl.Colors.Black = [ConsoleColor]::Black 130 | $sl.Colors.Blue = [ConsoleColor]::Blue 131 | $sl.Colors.Cyan = [ConsoleColor]::Cyan 132 | $sl.Colors.DarkBlue = [ConsoleColor]::DarkBlue 133 | $sl.Colors.DarkCyan = [ConsoleColor]::DarkCyan 134 | $sl.Colors.DarkRed = [ConsoleColor]::DarkRed 135 | $sl.Colors.DarkGray = [ConsoleColor]::DarkGray 136 | $sl.Colors.Gray = [ConsoleColor]::Gray 137 | $sl.Colors.Magenta = [ConsoleColor]::Magenta 138 | $sl.Colors.Red = [System.ConsoleColor]::Red 139 | $sl.Colors.White = [ConsoleColor]::White 140 | $sl.Colors.Yellow = [ConsoleColor]::Yellow 141 | $sl.Colors.BackgroundColor = $sl.Colors.DarkCyan 142 | $sl.PromptSymbols.ForwardHollowArrow = [char]::ConvertFromUtf32(0xE0B1) 143 | $sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276F) 144 | $sl.PromptSymbols.SegmentBackwardSymbol = [char]::ConvertFromUtf32(0xE0B2) 145 | $sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0) 146 | $sl.PromptSymbols.HeartSymbol = [char]::ConvertFromUtf32(0x2764) 147 | $sl.PromptSymbols.WarningSymbol = [char]::ConvertFromUtf32(0x203C) 148 | $sl.PromptSymbols.StartSymbol = '' -------------------------------------------------------------------------------- /install.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .Synopsis 3 | Install the TsekNet PowerShell profile. 4 | 5 | .DESCRIPTION 6 | Install the TsekNet PowerShell profile from GitHub for the desired scope. 7 | By default, the existing profile will not be overwritten. 8 | 9 | .Parameter Scope 10 | Name of the PowerShell profile scope to use. 11 | See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles for more information. 12 | 13 | .Parameter URL 14 | Fully qualified URL to the profile.ps1 file to download. 15 | 16 | .EXAMPLE 17 | Overwrite the AllUsersAllHosts PowerShell Profile with data downloaded from the URL. 18 | .\install.ps1 -Scope 'AllUsersAllHosts' -URL 'http://example.com' 19 | #> 20 | [CmdletBinding()] 21 | param( 22 | [ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')] 23 | [string]$Scope = "CurrentUserAllHosts", 24 | [Uri]$URL = 'https://github.com/TsekNet/PowerShell-Profile/raw/master/profile.ps1' 25 | ) 26 | 27 | $profile_dir = Split-Path $PROFILE.$Scope 28 | $profile_file = $profile.$Scope 29 | $request = Invoke-WebRequest $URL -UseBasicParsing -ContentType "text/plain; charset=utf-8" 30 | 31 | if (-not (Test-Path $profile_dir)) { 32 | New-Item -Path $profile_dir -ItemType Directory | Out-Null 33 | Write-Verbose "Created new profile directory: $profile_dir" 34 | } 35 | 36 | # Leverage IO.File for PowerShell 5 comppatability. 37 | [IO.File]::WriteAllLines($profile_file, $request.Content) 38 | Write-Verbose "Wrote profile file: $profile_file with content from: $URL" 39 | & $profile_file -Verbose -------------------------------------------------------------------------------- /profile.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | TsekNet PowerShell Profile. 4 | 5 | .DESCRIPTION 6 | Personal heavily customized PowerShell profile. Feel free to use and 7 | distrubute as you see fit. Expect frequent updates. Please file PRs for any 8 | errors/improvements. 9 | 10 | To use this profile, simply place this file in any of your $profile 11 | directories and restart your PowerShell console (Ex: $profile) 12 | 13 | Execution of functions can be found a the bottom of this profile script. 14 | 15 | .LINK 16 | TsekNet.com GitHub.com/TsekNet Twitter.com/TsekNet 17 | #> 18 | [CmdletBinding()] 19 | param () 20 | 21 | #region function declarations 22 | 23 | # Helper function to change directory to my development workspace 24 | function Set-Path { 25 | [CmdletBinding()] 26 | Param ( 27 | [ValidateScript( { 28 | if (-Not ($_ | Test-Path) ) { 29 | Write-Verbose "Creating default location $_" 30 | New-Item -ItemType Directory -Force -Path $_ 31 | } 32 | return $true 33 | })] 34 | [System.IO.FileInfo]$Path 35 | ) 36 | Write-Verbose "Setting path to $Path." 37 | Set-Location $Path 38 | } 39 | 40 | # Helper function to ensure all modules are loaded, with error handling 41 | function Import-MyModules { 42 | [CmdletBinding()] 43 | param ( 44 | [Parameter(Mandatory)] 45 | [string[]]$Modules 46 | ) 47 | 48 | foreach ($Module in $Modules) { 49 | if (Get-Module -ListAvailable -Name $Module -Verbose:$false) { 50 | Write-Verbose "Module '$Module' found, skipping install." 51 | Continue 52 | } 53 | try { 54 | Write-Verbose "Attemping to install module '$Module'" 55 | Import-Module -Name $Module -ErrorAction Stop -Verbose:$false 56 | } 57 | catch { 58 | $lookup = Find-Module -Name $Module 59 | if (-not $lookup) { 60 | Write-Error "Module `"$Module`" not found." 61 | continue 62 | } 63 | Install-Module -Name $Module -Scope AllUsers -Force -AllowClobber 64 | Import-Module -Name $Module -Scope Global -Verbose:$false 65 | } 66 | } 67 | } 68 | 69 | # Helper function to test prompt elevation 70 | function Get-Elevation { 71 | [CmdletBinding()] 72 | param () 73 | if (Test-Administrator) { 74 | $script:elevation = "Admin" 75 | Write-Verbose "Powershell is running as: $script:elevation" 76 | } 77 | else { 78 | $script:elevation = "Non-Admin" 79 | Write-Verbose "Powershell is running as: $script:elevation" 80 | } 81 | } 82 | 83 | # Helper function to set the window title 84 | function Set-WindowTitle { 85 | [CmdletBinding()] 86 | param () 87 | $host_title = [ordered]@{ 88 | 'Elevation' = $elevation 89 | 'Version' = "v$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" 90 | 'Session' = "$env:COMPUTERNAME".ToLower() 91 | } 92 | 93 | $formatted_title = "PS [$($host_title.Values -join ' | ')]" 94 | 95 | Write-Verbose "Setting Window Title to '$formatted_title'" 96 | 97 | $Host.UI.RawUI.WindowTitle = $formatted_title 98 | } 99 | 100 | # Download Files from Github 101 | function Import-GitRepo { 102 | <# 103 | .Synopsis 104 | This function will download a Github Repository without using Git 105 | .DESCRIPTION 106 | This function will download files from Github without using Git. You will 107 | need to know the Owner, Repository name, branch (default master), and 108 | FilePath. The Filepath will include any folders and files that you want to 109 | download. 110 | .EXAMPLE 111 | Import-GitRepo -Owner MSAdministrator -Repository WriteLogEntry -Verbose -FilePath ` 112 | 'WriteLogEntry.psm1', 113 | 'WriteLogEntry.psd1', 114 | 'Public', 115 | 'en-US', 116 | 'en-US\about_WriteLogEntry.help.txt', 117 | 'Public\Write-LogEntry.ps1' 118 | #> 119 | [CmdletBinding()] 120 | [Alias()] 121 | [OutputType([int])] 122 | param ( 123 | # Repository owner 124 | [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 0)] 125 | [string]$Owner, 126 | 127 | # Name of the repository 128 | [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 1)] 129 | [string]$Repository, 130 | 131 | # Branch to download from 132 | [Parameter(ValueFromPipelineByPropertyName, Position = 2)] 133 | [string]$Branch = 'master', 134 | 135 | # List of files/paths to download 136 | [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 3)] 137 | [string[]]$FilePath, 138 | 139 | # List of posh-git themes to download 140 | [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position = 4)] 141 | [string[]]$ThemeName 142 | ) 143 | 144 | $modulespath = ($env:psmodulepath -split ";")[0] 145 | $PowerShellModule = "$modulespath\$Repository" 146 | $wc = New-Object System.Net.WebClient 147 | $wc.Encoding = [System.Text.Encoding]::UTF8 148 | if (-not(Test-Path $PowerShellModule)) { 149 | Write-Verbose "Creating module directory" 150 | New-Item -Type Container -Path $PowerShellModule -Force | Out-Null 151 | } 152 | 153 | if (-not(Test-Path $profile)) { 154 | Write-Verbose "Creating profile" 155 | New-Item -Path $profile -Force | Out-Null 156 | } 157 | 158 | foreach ($item in $FilePath) { 159 | if ($item -like '*.*') { 160 | $url = "https://raw.githubusercontent.com/$Owner/$Repository/$Branch/$item" 161 | Write-Verbose "Attempting to download from '$url'" 162 | if ($item -like "*$ThemeName.psm1") { 163 | Write-Verbose "'$item' detected, overwriting..." 164 | $fullpath = "$(Join-Path -Path (Get-ChildItem $profile).Directory.FullName -ChildPath 'PoshThemes')\$ThemeName.psm1" 165 | if (-not(Test-Path $fullpath)) { 166 | Write-Verbose "Creating file '$fullpath'" 167 | New-Item -ItemType File -Force -Path $fullpath | Out-Null 168 | } 169 | ($wc.DownloadString("$url")) | Out-File $fullpath 170 | } 171 | elseif ($item -like '*profile.ps1') { 172 | Write-Verbose "'$item' detected, overwriting..." 173 | New-Item -ItemType File -Force -Path $profile | Out-Null 174 | Write-Verbose "Created file '$profile'" 175 | ($wc.DownloadString("$url")) | Out-File "$profile" 176 | } 177 | else { 178 | Write-Verbose "'$item' detected, overwriting..." 179 | New-Item -ItemType File -Force -Path "$PowerShellModule\$item" | Out-Null 180 | Write-Verbose "Created file '$PowerShellModule\$item'" 181 | ($wc.DownloadString("$url")) | Out-File "$PowerShellModule\$item" 182 | } 183 | } 184 | else { 185 | New-Item -ItemType Container -Force -Path "$PowerShellModule\$item" | Out-Null 186 | Write-Verbose "Created file '$PowerShellModule\$item'" 187 | $url = "https://raw.githubusercontent.com/$Owner/$Repository/$Branch/$item" 188 | Write-Verbose "Attempting to download from $url" 189 | } 190 | } 191 | } 192 | 193 | function Install-Fonts { 194 | [CmdletBinding()] 195 | param ( 196 | [System.IO.FileInfo]$TestFont = "$env:LOCALAPPDATA\Microsoft\Windows\Fonts\DejaVu Sans Mono for Powerline.ttf" 197 | ) 198 | if (-not(Test-Path $TestFont)) { 199 | Write-Verbose "Installing Fonts to $($TestFont.DirectoryName)" 200 | git clone https://github.com/PowerLine/fonts 201 | Set-Location fonts 202 | .\install.ps1 'Deja*' -Confirm:$false 203 | } 204 | } 205 | 206 | #endregion 207 | 208 | #region helper functions 209 | 210 | Write-Verbose "==Setting command aliases==" 211 | 212 | # Copy the last command entered 213 | function Copy-LastCommand { 214 | Get-History -Id $(((Get-History) | Select-Object -Last 1 | 215 | Select-Object ID -ExpandProperty ID)) | 216 | Select-Object -ExpandProperty CommandLine | 217 | clip 218 | } 219 | 220 | # Make it easy to edit this profile once it's installed 221 | function Edit-Profile { 222 | if ($host.Name -match "ise") { 223 | $psISE.CurrentPowerShellTab.Files.Add($profile) 224 | } 225 | else { 226 | code-insiders $profile 227 | } 228 | } 229 | 230 | # Open PowerShell command history file 231 | function Open-HistoryFile { code-insiders (Get-PSReadLineOption | Select-Object -ExpandProperty HistorySavePath) } 232 | 233 | # Compute file hashes - useful for checking successful downloads 234 | function Get-FileHash256 { 235 | $sha_256_hash = (Get-FileHash -Algorithm SHA256 $args).hash 236 | Write-Output "Hash for $args is '$sha_256_hash' (copied to clipboard)." 237 | $sha_256_hash | clip 238 | } 239 | 240 | function Get-ExportedFunctions { 241 | try { 242 | $helper_functions = (Get-Module $profile -ListAvailable | Select-Object -ExpandProperty ExportedCommands).Values.Name -join ', ' 243 | Write-Host 'Profile helper functions: ' -NoNewline; Write-Host $helper_functions -ForegroundColor Green 244 | } 245 | catch { 246 | Write-Error "Error obtaining helper function list: $_" 247 | } 248 | } 249 | 250 | #endregion 251 | 252 | #region statements 253 | 254 | # Hold shift to turn on verbosity if running Windows PowerShell 255 | if ("Desktop" -eq $PSVersionTable.PSEdition) { 256 | Add-Type -Assembly PresentationCore, WindowsBase 257 | try { 258 | if ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::LeftShift) -OR 259 | [System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::RightShift)) { 260 | $VerbosePreference = "Continue" 261 | } 262 | } 263 | catch { 264 | Write-Warning "Error displaying verbosity via SHIFT key." 265 | } 266 | } 267 | 268 | #endregion 269 | 270 | #region execution 271 | 272 | try { 273 | Write-Verbose '==Removing default start up message==' 274 | Clear-Host 275 | 276 | Write-Verbose '==Getting latest profile files from GitHub==' 277 | Import-GitRepo -Owner tseknet -Repository PowerShell-Profile -FilePath ` 278 | 'profile.ps1', 279 | 'Themes/TsekNet.psm1' -ThemeName 'TsekNet' 280 | 281 | Write-Verbose '==Importing modules required for profile==' 282 | $my_modules = @('posh-git', 'oh-my-posh', 'Get-ChildItemColor', 'PSWriteHTML') 283 | Import-MyModules -Modules $my_modules 284 | 285 | Write-Verbose '==Setting custom oh-my-posh theme==' 286 | Set-Theme 'TsekNet' -Verbose:$false 287 | 288 | Write-Verbose '==Checking console elevation==' 289 | Get-Elevation 290 | 291 | Write-Verbose '==Setting the console title==' 292 | if ($ThemeSettings.Options) { $ThemeSettings.Options.ConsoleTitle = $false } 293 | Set-WindowTitle 294 | 295 | Write-Verbose '==Setting the default directory for new PowerShell consoles==' 296 | Set-Path 'C:\Tmp' 297 | 298 | Write-Verbose '==Installing fonts if necessary==' 299 | Install-Fonts 300 | 301 | Write-Verbose '==Changing to bash-like tab completion==' 302 | Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete 303 | Set-PSReadlineOption -ShowToolTips -BellStyle Visual 304 | 305 | Write-Verbose '==Setting aliases==' 306 | Set-Alias ll Get-ChildItemColor -Option AllScope 307 | Set-Alias ls Get-ChildItemColorFormatWide -Option AllScope 308 | Set-Alias History Open-HistoryFile -Option AllScope 309 | 310 | Write-Verbose '==Getting and displaying list of helper functions==' 311 | Get-ExportedFunctions 312 | } 313 | catch { 314 | throw "Error configuring `$profile on line $($_.InvocationInfo.ScriptLineNumber): $_" 315 | } 316 | 317 | #endregion 318 | --------------------------------------------------------------------------------