r/PowerShell • u/StartAutomating • Jun 25 '26
Script Sharing Stop using [System]
I'm getting old enough that my fingers hate my lifetime of programming.
I'll save a few keystrokes where I can.
There's something simple most people don't seem to know about PowerShell syntax.
It saves seven characters of typing every you use this, and runs a tiny bit faster.
You never need to specify stuff is in the [System] namespace.
Stop Using [System]
.NET is a huge framework with tons of useful stuff in it. There's a lot of stuff in the System namespaces. Built-in framework functionality often exists in one of the many namespaces in System.
By the time PowerShell was being built, it was pretty clear that leveraging .NET was worth it, and that most people wouldn't want to type six to seven more characters every time.
So, since PowerShell v1, you haven't had to.
You can omit the [System] in any type in any system namespace
So instead of:
[system.collections.generic.list[string]]
We can write:
[collections.generic.list[string]]
Instead of:
[System.Collections.IDictionary]
We can write:
[Collections.IDictionary]
This is true for every system type. On my machine, there are 4722 public types in the system namespace. That's 33054 characters I will never have to type.
It makes scripts shorter and simpler to read.
Also, when PowerShell resolves types, it checks for the shorter names first. This saves a very tiny amount of time in each of your scripts. (I was corrected)
Yet, sadly, I see the system namespace everywhere in people's scripts.
I beg of you all:
- Save your fingers
- Make scripts shorter
142
u/Kemeros Jun 25 '26 •
I love how you wrote a wall to tell us: "type less" 🤣
Save a keyboard. Save a life.
26
u/Thotaz Jun 25 '26
This comment in the type resolver says otherwise: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/TypeResolver.cs#L368
And if you go through the rest of that method it seems to be true.
As for saving your fingers, an even better way is to use tab completion, which happens to include
System.unless you add ausing namespace System. If I want a generic list I type in[List<Tab>]which turns into[System.Collections.Generic.List]. So I'm sorry, but I'm going to continue using the full typenames.11
u/Nu11u5 Jun 25 '26
You can import any namespace with the
using namespacestatement at the top of the script.It's especially useful when using WinForms or WPF.
Personally, I use the fully qualified name unless I manually import the namespace. I don't like implicit references.
5
u/Hoggs Jun 25 '26
You're using a language that explicitly decided on day one, that they would prefer
Get-ChildIteminstead of the current standardslsordir.If you care about keystrokes you're using the wrong language.
In b4 "buuut aliases"
9
u/baron--greenback Jun 25 '26
So me think, why waste time say lot word when few word do trick?
2
u/narcissisadmin Jun 26 '26 edited Jun 26 '26
You could have saved your post and typed [system] 216 times. =P
37
u/MrMunchkin Jun 25 '26
If you're just writing scripts, sure this makes some sense.
But, if you're using it for anything complex or writing custom classes or wrappers for system methods, I will always use fully qualified names.
And if I'm going to do that, why not just be consistent and always use the fully qualified name?
If typing 6 words and a period is something that saves you time, I have questions on exactly what it is you do all day.
18
u/purplemonkeymad Jun 25 '26
I'm not typing system myself as I'm more or less always using auto-complete for the classes. It's more effort for me to remove it.
9
u/surfingoldelephant Jun 25 '26 edited Jun 26 '26
The following all generate code with
System:PlatyPSMAMLPlus there are some contexts outside of type literals where you must include it:
<TypeName>inTypes.ps1xmlandFormat.ps1xmlSo just from a consistency point of view, there's an argument to include it everywhere. Personally, I don't think any of the arguments are strong enough so choose to omit it.
6
u/AdeelAutomates Jun 26 '26
why stop with:
throw in at the top of your script:
and from there on just type:
profit? 😂
4
u/wickedang3l Jun 25 '26
Aliases make scripts and classes shorter too; I write for clarity, not brevity.
3
u/Mayki8513 Jun 25 '26
too add to your post with as little typing as possible:
1
u/Southpaw018 Jun 25 '26
I’m afraid you’re fighting a losing battle. People in this sub seem to be allergic to aliases, shortcuts, and generally anything that’s not maximum verbosity. I used to post scripts, but I got tired of getting told to take aliases out.
2
u/Nexzus_ Jun 25 '26
Yeah, I do it for autocomplete.
2
u/dathar Jun 25 '26
I got into a habit of typing out the whole namespace because
2
u/tadc Jun 26 '26
Honestly this is one of the things I like least about powershell... How unnecessarily and mandatorily (sometimes) verbose it is
3
u/SendMeSteamKeys2 Jun 26 '26
“This is true for every system type. On my machine, there are 4722 public types in the system namespace. That's 33054 characters I will never have to type.”
This right here is peak greybeard being tired of it all. Well said sir. Well said.
1
u/JVAV00 Jun 26 '26
Will do, I didn't know about that. I do know about dotnet tho.
1
u/Over_Dingo Jun 26 '26
So if I autocomplete '[list' I need to move back to remove 'system'. Doesn't seem to save me keystrokes
1
u/guy1195 Jun 27 '26
Bro must have no tab key surely???
This is automatic my dude. I type ArrayL and hit tab, and I get the entire system line.. 🤣
0
u/Romero126 Jun 26 '26
Use 'system' save compute time resolving relative namespaces using reflection.
-1
u/Unable_Attitude_6598 Jun 26 '26
Haven’t typed a lick of ps in months lmao
-1
u/whyliepornaccount Jun 26 '26
I'd love to. But certain UEM's documentation explicitly states that all scripts must run under system context cough
Tanium cough
6
u/[deleted] Jun 25 '26
[removed] — view removed comment
26
u/[deleted] Jun 25 '26
[removed] — view removed comment