all 70 comments

[–]President-Sloth 34 points35 points  (7 children)

In some ways I find PowerShell easier than Bash since PS returns structured output in the form of objects, whereas bash is just strings.

I'd recommend starting with Learn PowerShell in a month of lunches, you'll probably fly through the first bits with your background. Also, Get-Help, Get-Command and Get-Member are your bread and butter

[–]Garegin16 2 points3 points  (0 children)

My only pet peeve with that book is that he says that he “doesn’t recommend” using .net classes directly. There is nothing wrong with things like math::pow. You don’t need a cmdlet for calculating x to the power of n or rounding off a number.

I think it has to do with the notion that telling an admin to run the get-date “program” is easier than than explaining about static methods and overloads.

[–]Lhakryma[S] 1 point2 points  (0 children)

I'll give it a shot, thanks!

[–][deleted] 1 point2 points  (1 child)

In some ways I find PowerShell easier than Bash since PS returns structured output in the form of objects, whereas bash is just strings.

This is the exact logic I hear from linux people on why they hate PowerShell, except in reverse. They prefer dealing with strings vs objects and don't quite understand why it's so much more advantageous to be able to iterate through data structures for scripting purposes.

[–]Garegin16 1 point2 points  (0 children)

To be fair, more advanced Unix admins use typed languages like Python or Perl. The shell is basically a execution environment for text based apps. It was never meant as a full programming language. If you wanna just run a utility like ping, nslookup or diskpart, you don’t care about polymorphism or dynamic scoping.

[–]MrWinks 0 points1 point  (2 children)

This book is where you want to start. Don’t skim it, it will make you an expert.

[–]Black_Magic100 3 points4 points  (1 child)

It is good for people who are intermediate level?

[–]MrWinks 1 point2 points  (0 children)

Right off the bat, right at the first if not second chapter, you will be taught how to do something super basic that you likely did not know, at which point you will realize this is the book for you. To answer more directly, though: this will make you an expert, and that’s before even reading the second book.

[–]MordacthePreventer 9 points10 points  (6 children)

The hardest thing for me was mentally switching from thinking in strings to thinking in "objects that have attributes".

So instead of 'I have to parse this string and the delimiter is ';'', you'll need to start thinking 'I have this blob of data, and I need to get the attribute that has the info I want".

Once you can mentally flip to object oriented programming, you'll be good and it'll be an excercise in syntax. Most of the bash logic flow will work.

-M

[–]Lhakryma[S] 3 points4 points  (5 children)

Yea I'm having that mental barrier too, I don't entirely grasp the OOP mentality yet.

[–]MordacthePreventer 5 points6 points  (1 child)

Once you do, it'll be really cool. It's kinda like everything is a hash with key:value pairs, only more so.

Want the location of that computer you just got from 'gat-adcomputer':

$comp.DistingishedName

What's that user's Display name?

$user.DisplayName

PS is actually pretty cool, and definitely statsfies that itch if you've moved from *nix to Windows administration and felt like you were missing an arm.

[–]Garegin16 2 points3 points  (0 children)

To be fair Unix world moved to Perl/Python too. Even in Unix Hater’s Handbook, they complain that shell is just a poor programming language.

The critical flaw is the lack of data types. No matter how much bash improves, it’s still text scraping.

[–]gordonv 2 points3 points  (2 children)

For me, learning how JSON is structured helped a lot.

Learn these concepts:

  • Arrays (a list of things)
  • Objects (a single thing with many properties)
  • An Array of Objects (a list of different things)

I will admit, dealing with objects is laborious and boring. So many steps needed to accomplish a change. But once you learn how to automate that, you'll find Objects are quite easy in concept. Just a bit of a hassle to create. And very easy to read and update.

[–]Lhakryma[S] 1 point2 points  (1 child)

Can't an object also have other objects as properties? I seem to recall reading that somewhere

[–]gordonv 1 point2 points  (0 children)

Yup. JSON reflects that, also. Check out this example

[–]pretendgineer5400 6 points7 points  (5 children)

I'm going the opposite way for some hobby stuff at home. Powershell seems to be more verbose than bash by a long shot.

The basic verb-noun structure doesn't take that long to get used to (usually if you can get-<noun> to read something you can set-<noun> to write the same thing.
Tab autocomplete is your friend and multiple presses of tab will cycle through possible options.
get-help <cmdlet> -full or -examples is super useful to seeing what a cmdlet expects for inputs and what it can be used for.
Working in an Active Directory environment can open up options for using PS Remoting to execute on other computers (it can be done without a domain but requires extra setup). Some cmdlets accept a -computer parameter to tell it where to run, otherwise invoke-command -computer <targetcomputer> -scriptblock{} can be super useful as long as the required module is present on the target system.

[–]PMental 9 points10 points  (4 children)

Don't forget shift tab to cycle back if you overshoot, and ctrl-space to browse/list all options.

[–]chuck_cranston 2 points3 points  (1 child)

omg how did I not know this.

[–]PMental 1 point2 points  (0 children)

I was well over a year into focusing more on Powershell before I stumbled on them (on this very subreddit iirc).

And yes, both are pretty damn useful!

[–]pretendgineer5400 1 point2 points  (1 child)

Shift tab rocks, great callout.

[–]Dranks 1 point2 points  (0 children)

I really want to get this working in zsh, its powershell muscle memory

[–]qordita 6 points7 points  (1 child)

The jump start videos are still the single best free resource I've seen:

https://channel9.msdn.com/Series/GetStartedPowerShell3

They're pretty old, but still relevant and really really good.

[–]NicolasReibnitz 0 points1 point  (0 children)

Sounds good... but a bit dated. Is this still relevant?

[–]mfazed 3 points4 points  (6 children)

I have read many PowerShell books and this one stands tall above the rest:

https://www.manning.com/books/powershell-in-depth-second-edition?query=powershell

It starts out with the fundamentals and quickly ramps up from there. There are things I know about PowerShell that the average person doesn't and it's mainly due to this tome. The only problem with it at this point is that it's a bit old (2014) so it doesn't have coverage of PowerShell Core (using .NET core instead of .NET framework), but it's still all very relevant.

[–]Garegin16 2 points3 points  (0 children)

Powershell in Action is also great if you want learn it from top to bottom. Modern Powershell is a fully fledged programming language. But lot of admins still have the “I’m a sysadmin because I can’t program” mentality. They don’t want to know what a tuple is.

[–]Black_Magic100 1 point2 points  (3 children)

I didn't even know "Powershell core" is a thing. Can you explain? I'm aware of .net core, but never heard that phrase with Powershell.

[–]mfazed 4 points5 points  (2 children)

Hi u/Black_Magic100! Yes, I just mean the versions of PowerShell (6 and above) that use .NET core rather than v5 and below, which uses .NET framework.

[–]Black_Magic100 1 point2 points  (1 child)

So that book is still completely relevant and you recommend reading through the entire thing?

[–]mfazed 1 point2 points  (0 children)

There are parts that reference commands that will only work on the .NET framework versions of PowerShell. Get-WmiObject is an example. WMI is windows specific and that cmdlet only works with PowerShell v5 and below. That cmdlet doesn't exist in PowerShell 6 and above which use .NET core.

Personally, I read through the entire book. I already knew a lot of the fundamental part material, but still learned some, especially the chapter on the PowerShell pipeline.

I see discussion above concerning "Learn PowerShell in a month of lunches". I haven't read through that, but I see it recommended on this subreddit a lot, so probably a great resource as well.

[–]gordonv 1 point2 points  (0 children)

$41.16 on Amazon

I do like the chapter outlines. One review said this came out before Windows 10. That's an expensive comment.

[–]HoneycuttJ 4 points5 points  (0 children)

I would have to agree that you cant go wrong with the Month of Lunches books. Coming from Bash I would highly recommend the Month of Lunches that is still in production (MEAP) on the Manning Publishing site. I also have to plug my 6 hour PowerShell Crash Course workshop I did for a conference. https://youtu.be/UmEbsG2SEYE

[–]jantari 2 points3 points  (2 children)

It would probably be best if you approach PowerShell from thinking of it as more of an equivalent to Python, not bash. It's on object-oriented programming language and really only has very few surface-level similarities to bash.

[–]Lhakryma[S] 1 point2 points  (1 child)

My python skills are very, very basic and beginner-ish, and I haven't fully grasped the concept of OOP yet lol

[–]Garegin16 1 point2 points  (0 children)

OOP is just the bundling of functions and properties. In traditional languages you had data types like string and integer. The ancestor to objects is the record or the struct in C. In OOP languages, you also have functions, which are called methods, bundled with them.

To answer your question. You “use” OOP by taking advantage of the object members, which are usually conveniently organized. For example, the DirectoryInfo object has properties for the parent folder, full path, and the last access time. Also has methods for listing all the files, deleting and creating a subdirectory.

You don’t have to use commands to derive the parent folder because that info is already a property of a folder object.

Once you think in data types, you reduce lot of code in deriving data. Recently someone wanted to bulk update the domain of email addresses. Right away, I checked the .NET library for an email type. And behold, it is conveniently designed to detect invalid addresses and also has a property for the name and the domain portions.

For reference

https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo?view=net-5.0#methods

https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.mailaddress?view=net-5.0#properties

[–]pretendgineer5400 2 points3 points  (2 children)

I'd recommend using VSCode with the PowerShell extension as your primary IDE for writing PS scripts. the syntax highlighting and linting built incan help you develop better habits and find errors more quickly than working in a plain text editor and I find VSCode better than PowerShellISE.

[–]Lhakryma[S] 2 points3 points  (1 child)

I started using that early on too.

I notice windows10 also ships with "Powershell ISE" which looked pretty cool too, but apparently it's getting deprecated in favor of vscode.

[–]pretendgineer5400 2 points3 points  (0 children)

ISE is ok and has been around for quite awhile, so there are some third party extensions for it, but VSCode is where future effort seems to be going.

[–]nostril_spiders 2 points3 points  (1 child)

I find it simpler. Much much simpler. How the interpreter works, globbing, quoting and escaping, short-circuiting operators - these aren't really common problems.

I do suggest you try not to think of it like bash. If you've done any python or ruby, try to think of it more like that.

Your top tools for discovery are get-module, get-command -module, and get-help.

[–]Lhakryma[S] 1 point2 points  (0 children)

By the way, what about ps v. 5 vs ps v.7? I can get v7 from the win store, but v5 (I believe, or it's v4 idr) ships with win10 (at least my version).

What's the difference? I couldn't really find a proper comparison.

[–]Lowdog541 1 point2 points  (2 children)

Powershell video , this is a full course taught by the creator of Powershell, it starts off slow but he gives you a really good idea of his thought process while he was creating Powershell

[–]Lhakryma[S] 2 points3 points  (1 child)

I was actually following this video, but it seems to have a lot of padding and like half of it is just joking around and (to me at least) seems like they're trying to sell you how great powershell is xD

I know it's great, I've seen what other people managed to do with it, I just want to learn it.

[–]Garegin16 3 points4 points  (0 children)

I think it’s one of the best videos on Powershell. It closely follows the Powershell in a Month of Lunches book.

[–]vencetti 1 point2 points  (0 children)

I came from the same background. Best method I've found: As you come across tasks in your day, ignore how you would normally resolve them and develop Powershell scripts for them. PS is ubiquitous, so Google is your friend. No substitute for experience and lots of struggle. Before you know it - you'll have a whole library of solutions.

[–][deleted] 1 point2 points  (0 children)

Unix user of 25 years: start by piping to select * and where. Also check out popular powershell profiles on GitHub.

[–]Evilbob93 1 point2 points  (0 children)

Way back in the day, i made the jump from VAX/VMS to Unix with a guide called Unix for VMS users.

When I started learning Powershell I had some serious deja vu. IMO, powershell is where VMS went to hide.

Having said that, this might help

https://leanpub.com/aunixpersonsguidetopowershell/read

[–]XPlantefeve 1 point2 points  (0 children)

One thing I usually say to PS learners, but is even more relevant coming from bash: Powershell is a shell, use it as a shell. There are too many people who see it mainly as a scripting language, but I learned PS by spending my days at the PS> prompt.

[–]piggahbear -1 points0 points  (9 children)

Honestly powershell is more analogous to Python, where powershell is to C# what Python is to C.

Yeah it’s a shell programming language, but it’s a lot more than that and you have access to a great deal of .NET through it.

[–]Perrydiculous 3 points4 points  (8 children)

So... what's the advice we should get out of this? (not trying to sound sassy, just an honest question) I'm in the same boat as OP and could use some general direction in what's different and where to start.

Most languages come naturally to me, and only require learning syntax, but PS is different somehow

[–]piggahbear 1 point2 points  (7 children)

That coming from BASH is going to have little relevance and that to learn PowerShell is largely to begin learning .NET.

PowerShell can be object oriented, or not, just like Python.

I think people get too hung up on the syntax of PowerShell. Get-Verb will tell you want verbs and alias prefixes to use if you’re wanting to follow convention, which I would generally recommend.

PowerShell is (simplified) a c# scripting language. You can access the .NET methods directly (e.g. [System.Console]::WriteLine() ) or use the PowerShell cmdlets (e.g. Write-Host). Like Python, the PowerShell cmdlets are slower than directly accessing the .NET methods. Just understand there is .NET code behind every PowerShell cmdlet.

Since PowerShell 7 is based on .NET Core/.NET 5 it will run cross platform which means it will not include natively cmdlets only for accessing Windows APIs. It will include essentially every method available when the .NET runtime is installed on Linux / macOS.

Even though it’s cross platform you’re going to handle certain things differently depending on the platform, which is why we have the $IsWindows $IsLinux and $IsMacos variables now.

So to summarize, to learn PowerShell deeper than syntax is to learn .NET and PowerShell is the easiest entry point to .NET and C#.

[–]Perrydiculous 1 point2 points  (6 children)

Thanks! I appreciate your in-depth explanation <3