Hacker News new | ask | show | jobs
by MassiveBonk51 1026 days ago
This seems great. On a side note, I've been trying to learn powershell lately since I'm stuck on Windows at work. I'm not allowed to download utilities for powershell, and it feels anemic to me compared to a unix shell. An example of this is when I wanted to work with Json using ConvertTo-Json and the output kept getting mangled even though it was changing the right fields. Eventually I gave up and wrote a node script and just called it from my ps script. Is there a solution for making powershell actually enjoyable to use?
12 comments

Powershell does take a while to become fun. Key thing (I assume you know this?) is that while bash pipes a string-stream, powershell pipes a stream of structured objects. This is powerfull but also quite confusing sometimes.

The other key thing is that Powershell has straight access to the whole .NET framework (or whatever its called these days) except you've gotta switch to .CallSomeMethod(with,brackets) syntax for that.

This list of common Gotchas on SO was useful to me: https://stackoverflow.com/a/69644807/22194

Powershell is quite good at working with CSV: Import-CSV, Export-CSV, Where-Object, Select-Object, Sort-Object, Group-Object etc become quite handy when used together.

I've been writing powershell for 5 years and I'd rather slice my balls up with glass and dive into the red sea. How much longer before it becomes fun?
At least another 5 years I'm afraid
My poor balls
Windows PowerShell targets the old .NET Framework, which is Windows-dependent. Modern PowerShell targets .NET [Core] and is cross-platform. Also, it's not true that you need to use "CallMethod;" PowerShell can invoke methods with syntax nearly identical to C#'s.
edited my comment above for clarity - I didn't mean literally CallMethod, I meant that normally Powershell uses 'shell' syntax where parameters are separated by spaces

Do-Something withthisfile.txt -AdditionalParam 23

But if you're calling a .NET method you switch to C# syntax with brackets and commas:

$str.Trim('-')

I mean thats kindof obvious but its one of the many things that makes Powershell's learning curve a little steeper. But hey we can always just google it which is par for the course anyway nowadays

Not sure, but I think the above commentor was meaning to tell that powershell supports .NET reflection.

That is, while you can call .NET methods on objects like:

$Object.SomeMethod('arguments')

You can also do:

[Namespace.ClassName]::SomeMethod('arguments')

And your Powershell scripts can even save you a lot of typing with the ```using``` function of Powershell to load namespaces: https://learn.microsoft.com/en-us/powershell/module/microsof...

Edit: As to why you would want to do this, usually it's because you want to do some scripting but you need to pull data out of some .NET application or out of Windows and there isn't a fully fleshed out cmdlet built for it. In a pinch, you can usually just use reflection to get it. For example, I needed to pull data from a Shared Outlook calendar that not everyone had access to and then do some date-math to share a time-sensitive schedule with a team living in quite a few timezones. Reflection to load the necessary Outlook and MSExchange elements, then the rest in Powershell. Now I have a script that is easy to pass around, can do this if I'm unavailable, and we can easily add/remove time zones/persons, and it just needed 5-ish lines of .NET inside of the Powershell script.

Or if you just want to parse date time in a script.
Not only .NET, any COM library or DLL as well.
Love how many people are telling you "don't use powershell" instead of actually helping. Some tips:

- Powershell isn't case sensitive. Typing `ConvertTo-Json` is annoying, but you can write `convertto-json` instead.

- `alias` tells you all the built-in aliases.

- The autocomplete is really, really good. Press ctrl-space to get all the options to tab complete commands, flags, or flag values. IE you can pipe something to `select -prop` and autocomplete all the fields of the thing. Also you can autocomplete with wildcards, like `*Json`.

- Flags don't need to be written out in full, you can write `select -exp` instead of `select -ExpandProperty`.

- Pipe stuff to `Out-Gridview` to get a filterable, sortable popup!

`Out-Gridview -PassThru` is pure gold!
I found nushell (https://www.nushell.sh) to be an impressive replacement "bash" for Windows

In terms of philosophy, think "Powershell but actually intuitive" : Every data is structured but command names are what you expect them to be. I usually don't even need to look at the documentation.

I liked it so much that I also replaced my shell on Linux with it, so I have the same terminal experience across all OSes

It's because Powershell is a bit anemic. Coming from bash to Powershell at first, I disliked PS because I couldn't use it like bash; handling text was a pain, the moment you try to get fun with PS like you might with bash Windows will absolutely slap you with permissions or restrictions, etc.

Powershell shines best when you're automating _Windows things_ or anything .NET adjacent. If you play inside the very specific rules Powershell allows, it works great and your Windows experience will likely be nicer.

So if you find yourself working on Windows and saying to yourself "wow, this works great but I wish I didn't have to babysit it like this", PS is your friend.

If you need to automate Active Directory, Exchange, MSSQL (I guess some general maintenance things or quick queries without SQL Management Studio), etc, PS is your friend.

Anything else, and you basically need to figure out how to get around the restrictions Microsoft put in to prevent you from doing this, usually using .NET code in your PS script.

There's a huge amount of powershell support for admins on MSSQL.

e.g. https://dbatools.io/commands/ https://github.com/microsoft/ReportingServicesTools https://learn.microsoft.com/en-us/rest/api/power-bi/

Also all the normal Active Directory and file system tools, but those are too broad to go into.

Are you using Windows’ built-in Powershell? If so, are you allowed to install the newer Powershell Core (based on .NET Core)? The latter has a lot of fixes and improvements compared to the built-in Powershell.
i dont think its called core anymore its just called powershell 7 now

and dotnet core, is just dotnet

i think they dropped the core part from the name as of dotnet 5

On unix you would have had to write that script anyways unledd maybe jq can help (?). I usually use convertto-csv and it works like a charm. Powershell is more like python than bash.

If you learn .net stuff it might make it more easy to use. You can access .net functions from powershell, so had you written that code in c# instead if node, you could reuse it in powershell anytime you want.

Although, personally I use shells to test stuff or for basic things, python kicks butt regardless of platform.

I hope your work let's you use wsl. If not, try running powershell.exe -version 1, that let's you bypass constrained language mode if that is the issue. Also, lookup set-executionpolicy and unblock-file, in case those are what you needed for the not allowed part. You can even bring your own powershell entirely! Powershell itself is an interface around System.Management.Automation windows/.net assembly. There are tools used in red-teaming where you just bring your own powershell interface and bypass any restriction. Even if you can't bring new exe's, no problem, you can relectively load assembly's and run them in memory, with or without powershell. Your org must be setup Crazy good if you have windows and there is no way to do whatever you want in powershell without admin rights. But don't get in trouble because of my advice!

> since I'm stuck on Windows at work

Are you allowed to install Cygwin? It does a lot for making Windows a civilized environment.

A really powerful feature in PowerShell is to download MSYS2 and then launch a proper shell :)

I started to learn PowerShell and probably will still have to proceed, but I so far successfully avoided it in my current job. MSYS2 for sometime was a relief (WSL was a bit crippled on my company's internal network)

Cygwin aside, WSL became pretty good actually.
WSL2 has some network shenanigans that make it fail when I'm on the corporate VPN. Cisco, MS and corporate IT say they are on it. They've been "on it" for the past 20-ish months.
https://github.com/sakai135/wsl-vpnkit fixes that problem for me, and is less annoying than other fixes I tried
If you're not allowed to install anything, asking for git automatically comes with git-bash, so it provides some cover for the real reason. And hey, bonus, you get git!
That’s how some colleagues dealt with the corporate network shenanigans that crippled WSL.
Related tangent: I'm forced to suffer Windows at work too, and have found that git-bash makes the CLI at least mostly tolerable. (It's still painfully slow and inefficient compared to zsh in iTerm on my personal M1 macbook, but it works.)
I love Windows, .NET, and Azure. Genuinely. But I use Bash. Powershell has a lot of what seems like custom-syntax for every little thing and I can't be bothered to memorize it.

Also, you guys can probably install GitBash and claim it just came with Git...

Actually you need to memorize million times less stuff to use powershell comparing to bash. This is because everything is a string in bash, so you have to learn a custom DSL for each task. Want to parse a JSON? Learn jq. Want to parse XML? Learn xmlstarlet. etc. In powershell you just use ConvertFrom-JSON to convert json to object and then use normal powershell syntax to access object properties instead of using custom DSL. That's the problem with bash - learn million DSLs for each file type, format, etc.
powershell's json handling leaves a lot to be desired. It's been awhile since I poked around but I never found a good parser and usually just switch to python for that and basically everything else to do with APIs (invoke-webrequest is really annoying to use also).

powershell is good for controlling the windows operating system and doing basic things with objects. sysadmins are more likely to have a decent time with it than developers. Although, you can use it to interpret dotnet which is kinda neat.

    $myJson = Get-Content .\test.json -Raw | ConvertFrom-Json 
    $myJson.Accounts.Users.asmith.department = "Senior Leadership" 
    $myJson | ConvertTo-Json -Depth 4 | Out-File .\test.json 
from: https://techcommunity.microsoft.com/t5/core-infrastructure-a...
And the depth maxes out at 100, for some reason. This is my problem with most PS data things, they work well on the surface but when you really get your hands dirty they run like dogs or have weird limitations.
You may need to format the output

Read powershell in a month https://www.amazon.com/Learn-Windows-PowerShell-Month-Lunche...

Microsoft had a good course for it, idk if this is good Youtobe has too https://learn.microsoft.com/en-us/training/modules/introduct...

I like powershell but I also dont use python or bash or whatever

>making powershell actually enjoyable to use

My solution was to stop using it and instead use dotnet-script

https://github.com/dotnet-script/dotnet-script

Scripting with the full power of modern C# has been a huge win for me. And same/similar scripts will work on Windows/Linux/Mac. As my work language is C#, I don't have to context switch to another language for scripting.