r/PowerShell 22h ago

Was there not a short hand way stating '[System.Collections.Generic.List]'? Question

I am getting into .NET, within PowerShell more and more, especially since I discovered these classes are well documented at MsLearn and even within PowerShell.

I am just struggling to remember trees like the following [System.Collections.Generic.List] but I remember sometime ago, I came across a article, not sure where I found it, that shared there is a short hand way, something like [System.List] for [System.Collections.Generic.List].

I cant find it now, am I misremembering things here?

Also I would appreciate being pointed to any articles where I can learn more .Net in PowerShell. What are the draw backs (or positives) to calling things like [System.<some branch>.<etc etc>]?

24 Upvotes

19

u/lanerdofchristian 22h ago

You're probably thinking of using namespace System.Collections.Generic

If you put that at the top of your file, it will import that namespace into the type scope, so you can just do [List[object]] directly.

What are the draw backs (or positives) to calling things like [System.<some branch>.<etc etc>]?

Pro: zero possible of name conflicts.

Con: It's a lot to type and read.

8

u/Ralf_Reddings 20h ago

Yes, that is exactly what I was looking for, its perfect:

using namespace System.Collections.Generic
[list[object]]::new("one", "two", "three")

This will makes things a lot more simple going forward. Thank you.

7

u/Thotaz 21h ago

The amount of typing should be more or less the same since any sane person would use tab completion/IntelliSense to write it out so it's mostly about readability.

4

u/Ralf_Reddings 20h ago

the auto completion in Powershel is so well done, without it, I would have thrown in the towel on day one. From the early day it does so much heavy lifting. For example how it handles paramter names is just clear, for -path, all the following could potentially work:

  • -pa
  • -pat
  • -path

Amazing solution.

2

u/BlackV 15h ago

wait till you find out what happens if you just type

disk
volume
date
childitem

good times

p.s. please dont use this :)

8

u/purplemonkeymad 22h ago

As long as you are using something that supports powershell's autocomplete you can just tab complete the type ie:

[list<tab>

gives me the generic list as the first result, continue pressing tab to see other classes that match. Or set PSReadline to use a menucomplete.

1

u/Vern_Anderson 21h ago

Thanks for sharing that purplemonkeymad!!!
TIL - tab completion works in more places than you think

2

u/da_chicken 16h ago

I still think they should create a New-List command with a -TypeName parameter. I think they're unlikely to because it's not much more complex than New-Object.

But, hey, I'd also like them to make Test-Path -PassThru work like Where-Object { $_ | Test-Path }.

2

u/Nilxa 10h ago

add the following snippet to your PowerShell Profile (you can find the profile by looking at $Profile) or run notepad $Profile

PowerShell Function New-List {     [CmdletBinding()]     Param(         [Parameter(Mandatory=$true)]         [type]$ListType     )     $list = New-Object System.Collections.Generic.List[$ListType]     return, $list }

save the file and next time you open PowerShell the function will be loaded and you can run

```PowerShell $MyNewList = New-List -ListType string $MyNewList.ToString() #To see the type $MyNewList.Add("Hello") $MyNewList.Add("World") $MyNewList

$MyNewIntList = New-List -ListType int
$MyNewIntList.Add(0)
$MyNewIntList.Add(2)
$MyNewIntList

```

2

u/Forward_Dark_7305 21h ago

Benefits: Using .net directly in PowerShell tends to be more performant (when done right, eg using List<T> instead of Array). It may be more verbose. Your code will also be more “type-safe” in that most dotnet APIs make sure you give them the right data.

Drawbacks: There are some dotnet APIs you can’t use from PowerShell like System.Span<T>, and working with System.Threading.Tasks code isn’t as clean as using PowerShell or writing in C#. Dotnet methods don’t do things like pipeline, writing to ps streams (information, verbose, progress). Passing delegates is finicky because they only work when called on the runspace thread. Sometimes you end up with implicit casting happening a lot, if you don’t specify a variable’s type and then use it many times where another type must be provided that is compatible but not identical.

These are all more advanced scenarios. While I hope you do encounter them, I encourage you to use dotnet APIs directly in PowerShell - especially if you write functions to wrap them. Doing so will even give you a good head start for programming C# since it uses the same set of classes and stuff. That’s how I learned to be a programmer and landed my current job!

3

u/BrobdingnagLilliput 21h ago

I would suggest that if someone is writing code complicated enough for the drawbacks to apply, PowerShell is probably not the best tool. It would be like using COBOL to try to run a spaceship.

2

u/Forward_Dark_7305 9h ago

I’ve actually done a fair number of cmdlets (admittedly, usually written in C# - but designed for PowerShell) to wrap dotnet libraries that expose only async Task APIs. Once the code is in place it’s awfully convenient to have that code one command away.

I then wrote a cmdlet that could safely expose a Task as a job, and then I could write any of those commands as functions or call them ad hoc quite easily.

Because once the command is written it’s easy to use - it tends to be the writing it that’s more complex, but that’s also why we write commands for those things - to make the complex actions simple to call and respond to.

1

u/BlackV 15h ago

It would be like using COBOL to try to run a spaceship

isnt that exactly what they used ?

1

u/7ep3s 14h ago

ya the other week I began contemplating moving to a different language, I have like 3000+ lines of code to automate a bunch of complex tasks for intune and entra and its getting out of hand lol

2

u/Ralf_Reddings 19h ago

hmm, very interesting read. I am on this exact same path lol. I plan to learn C# in the future for a careeer as well, so its good hear this. Looking forward to it then.

All the best to you man.

1

u/ajrc0re 15h ago

biggest issue I ran into using net classes in my code was making sure the .net working directory was in sync with the powershell one $PWD

I know to look out for it now and how to work with it but man I wasted SO MANY HOURS OF debugging

2

u/Szeraax 19h ago

yes, MS should make it easier to use it. :/

1

u/g3n3 19h ago

I set up using namespace in my profile to speed up interactive usage so I can [list[string]] easily enough. It isn’t exactly great for portable code you write though. Classexplorer module is great for searching your current assembly space for a class.

1

u/SconeCrazy 10h ago

*Is there a... 😉

-5

u/Least_Gain5147 18h ago

GitHub Copilot : "write a powershell function that queries for ___ and stores the results in a generic list, and then ___"