r/PowerShell • • Apr 06 '21

Optimizing your $Profile

https://devblogs.microsoft.com/powershell/optimizing-your-profile/
119 Upvotes

24 comments sorted by

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.

4

u/justinwgrote Apr 07 '21

Have you used Powershell 7? Significant improvements were made to the boot time.

2

u/jantari Apr 07 '21

While that's true, I still wish startup was at least twice as fast even in v7

2

u/Thotaz Apr 07 '21

Yes and it's still worse than CMD.

1

u/HeKis4 Apr 07 '21

True. What really kills me is the time it takes for the first tab-completion of a session if you're tab-completing something from a module, like Get-ADUser -Prop *tab*. I could have typed the LDAP query faster than it takes to complete lol.

I think the console basically tries to parse your entire $PsModulePath when you do that, which takes ages especially on HDDs (don't ask me why I'm still using an HDD as a system drive in 2021). This is something that could be optimized a lot, I'm not sure I need my VMWare module ready to go if I'm doing AD work.

0

u/rmbolger Apr 07 '21

I mean, tab completing something from a module means it needs to actually import that module before it can know what to tab complete. And the AD module in particular is rather glacial on initial import. I'd guess it's primarily because it tries to setup the AD:\ drive connection to your default domain and it's even slower if you're on a non-domain joined machine.

0

u/Thotaz Apr 07 '21

The slow tab completion in this situation is because it has to import the module to figure out what parameters are available for the command. If you import the AD module in your $Profile you won't see this happen but this will of course make the initial start up slower.

-5

u/Where_Do_I_Fit_In Apr 07 '21

The starting speed of PowerShell and cmd side by side is pretty noticable (especially with a powerful PC). I don't really see a compelling reason to start PowerShell other than using cmdlets/scripting. cmd does the job and starts faster.

7

u/da_chicken Apr 07 '21

You just start Powershell and leave it open. You should close it when you need to reset the environment. If you don't know when you need to do that, you probably don't need to close it at all.

Besides, once you get used to Windows Terminal, you just don't think about opening a new tab.

2

u/elevul Apr 07 '21

Eh, no windows terminal on servers

1

u/[deleted] Apr 07 '21

[removed] — view removed comment

0

u/jantari Apr 07 '21

That's a preview release only currently. Also not sure whether it will work on Server Core or just Desktop. If it requires the desktop then it's useless

1

u/Thotaz Apr 07 '21

There's no way it'll support core. Core doesn't have all of the modern UWP/XAML components that it uses.

0

u/Where_Do_I_Fit_In Apr 07 '21

You just start Powershell and leave it open.

I tend to open a new shell (cmd/bash/ps) in a directory by right clicking in File Explorer, so this doesn't really work for me personally.

36

u/da_chicken Apr 06 '21
Set-Content $Profile -Value $null

Profile optimized.

5

u/gramsaran Apr 07 '21

powershell.exe -noprofile

2

u/motsanciens Apr 07 '21
[System.IO.File]::WriteAllLines($Profile, "")  

Optimized profile optimization
;)

2

u/HeKis4 Apr 07 '21

Workflow deoptimized :p

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

u/endowdly_deux_over Apr 07 '21

I do love Alacritty.

-4

u/Cr82klbs Apr 07 '21

This optimization is "replace Powershell with .NET"