r/PowerShell • u/PowerShellChallenge • Apr 06 '21
Optimizing your $Profile
https://devblogs.microsoft.com/powershell/optimizing-your-profile/36
u/da_chicken Apr 06 '21
Set-Content $Profile -Value $nullProfile optimized.
5
2
u/motsanciens Apr 07 '21
[System.IO.File]::WriteAllLines($Profile, "")Optimized profile optimization
;)2
2
u/Banzai51 Apr 07 '21
I have my PowerShell_ISE setup to set some variables for my Citrix Site DDCs (I can use $SiteName in my scripts to point to the correct Citrix Site), load up the Citrix PowerShell commands, load PowerCLI and log me into both vCenters.
It may not be a fast start up, but I open it in the morning and I can run scripts or commands against my environment with ease.
2
u/motsanciens Apr 08 '21 edited Apr 08 '21
I got a lot of mileage out of this post, not the least of which is the Measure-Script tool, which is pretty sweet.
I couldn't help but try to re-invent the wheel to try to improve on the idea. I'm not sure I came up with anything novel, but there could be some things in here that spark some ideas for anyone who comes along.... https://pastebin.com/5iBXXS9r
One thing I did that may not be immediately obvious is include a file in the gist called "version" that just contains, you guessed it, the version. To me, it made sense to make a call for the version instead of pulling the whole profile script and then pattern matching to get the version. You end up with two http calls, but it's only when there's a real need to update.
I also included a way to turn the profile updater to manual or auto, and I had a lot of fun (yes, let's call it that) with scriptblocks along this journey.
Edit: After spamming [Enter] at the prompt and locking up my system, I added a limit of 5 background jobs at a time.
1
u/endowdly_deux_over Apr 07 '21
Holy crap.
I just went through my (really complex) profile.ps1 and just added one function: function Invoke-Combine { [System.IO.FileInfo] [System.IO.Path]::Combine.Invoke($args) }
I replaced all my Join-Path $x $y -OutVariable $z | Test-Path calls with that and the 'Exists' property and whoa.
Before: ~4500 ms
After: ~750 ms
I've always used .NET methods more anyway out of habit and familiarity but like cmdlets because they're pretty. I made my profile pretty. But pretty be damned, now. I like the extra 3 seconds off my day. I'll take that extra hour every 5 years.
0
u/Underxp Apr 07 '21
You could try Alacritty. It starts powershell more quickly than Windows Terminal.
1
-4
34
u/Thotaz Apr 06 '21
I wish PS was faster to start in general. Even without any profiles the first launch after a fresh boot is pretty slow regardless of how fast your PC is. On slower/busier PCs it can easily take several seconds and it makes me pick cmd over PS for simple tasks like viewing the IP address or launching a program from a folder with special arguments.