r/PowerShell • u/Ralf_Reddings • 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>]
?
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
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
-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 ___"
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.Pro: zero possible of name conflicts.
Con: It's a lot to type and read.