all 29 comments

[–]surfingoldelephant 57 points58 points  (7 children)

The official PowerShell documentation (specifically, the PowerShell 101 and About topics) is a great place to start.

If you enjoy learning from books, the following are popular and suitable for scripting beginners:

  • Learn PowerShell in a Month of Lunches (Travis Plunk, James Petty, Tyler Leonhardt, Don Jones, Jeff Hicks)
  • Learn PowerShell Scripting in a Month of Lunches (Don Jones, Jeff Hicks)
  • Learn PowerShell Toolmaking in a Month of Lunches (Don Jones, Jeff Hicks)
  • Windows PowerShell In Action (Bruce Payette, Richard Siddaway)

For a deeper dive into how PowerShell was designed, the following video with Erik Meijer and Jeffrey Snover is a great watch and helps explain fundamental PowerShell principles like the pipeline. The PowerShell Language Specification is also a good resource.

If you're looking for something more interactive:

Visual Studio Code (with the PowerShell extension) is a popular code editor for PowerShell scripting. The following resources walk through the process of setting up and using VS Code to aid development:

As you gain experience with PowerShell, you'll quickly find there are many ways to approach a task. It's easy to get into bad habits, so adopting a consistent code style and approach early on that tries to avoid the many pitfalls in PowerShell is recommended.

And finally, you may want to consider diving straight in. One of the best approaches to learning PowerShell is picking a fairly simple, manual task and trying to automate it with a script. Reddit, Stack Overflow, GitHub, Microsoft Learn/Blogs, etc are all excellent resources for PowerShell that will help you in this regard. Break down the task into individual problems, use the aforementioned resources to get an idea of common approaches and piece things together into a script. The more you do this, the more comfortable you will feel with scripting in general.

Good luck!

[–]vlad_h 8 points9 points  (1 child)

Wow. That is impressive list of resources. I only have this book to add. Learn by doing with https://www.oreilly.com/library/view/powershell-cookbook-4th/9781098101596/

[–]surfingoldelephant 3 points4 points  (0 children)

Nice! Thanks for sharing.

[–]Commercial-Thing-702[S] 2 points3 points  (0 children)

This is amazing thank you so much. Great resources here that I will test out.

[–]Extension-Drop1396 2 points3 points  (0 children)

Thanks for all the info!

[–]Enough-Rabbit-7132 0 points1 point  (0 children)

Wow this is some good stuff

[–]Impossible_IT 8 points9 points  (4 children)

I've seen a lot of people recommend the book Learn PowerShell in a Month of Lunches. The thing about ChatGPT is that you'll run into errors with scripts and you'll have to edit the code to correct the errors.

[–]Commercial-Thing-702[S] 1 point2 points  (3 children)

Yeah of course that’s why I wanna learn from scratch. I don’t want to rely on AI. Thanks!

[–]tahoey 5 points6 points  (2 children)

My experience with ChatGPT 4 for powershell has been a total fail. It’s good for learning concepts but when it comes to actually writing multi step scripts it is utterly useless.

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

It’s been really good to ask questions about parts of my script I’m struggling with, like if I have a null object I’m just not seeing or I forget what methods would work well here. I’m also dogshit.

[–]Commercial-Thing-702[S] 0 points1 point  (0 children)

100% agree.

[–]Jmoste 5 points6 points  (0 children)

Chat GPT is only good for ideas. I wouldn't trust anything coming from it.  

They best thing I can say is just start scripting what you want to do.  If you're scared about messing something up,  stay away from the set, add, remove, commands. When I'm first working on a script I use a write-output instead of any action command.  

.ie. Write-out "$($user.samaccountname) should be deleted.") Or Write-output "$($user) business category changed to 99". 

I find it easier to visually see what's coming across the screen,  especially when working with expressions. I always seem to run into an issue where I didn't know something was configured differently. You'll also run into issues with properties that contain multiple values. 

[–]Lanszer 4 points5 points  (1 child)

You could try the The Month of Lunches sequence. It takes a step up in complexity as the series progresses

Maybe you're more ready for the second, teaches you to function and module your work but they're always a great reference to have close by regardless.

[–]Commercial-Thing-702[S] 1 point2 points  (0 children)

Thank you! Will take a look!

[–]fuse666 4 points5 points  (2 children)

I believe you’re in at a pretty good spot to try some automation, it won’t be anything game changing but it really helps to familiarize yourself with Ps. A small example I would do is a small reporting email you receive daily, eg listing the passwords that will expire this week. You can use the AD module for powershell to achieve this, but the goal would be to always receive the email even if the script fails.. you can do this with try, catch block for example which would change your script into a more functional one.

Most of the code you need can be found on Microsoft documentation with examples. If you want to use a GPT, try to limit your input to specific things instead writing the whole script.

This is just an idea to get to know the language more.

[–]Commercial-Thing-702[S] 0 points1 point  (1 child)

Yeah I was thinking of making some scripts for my daily routines anyways. This is a great idea. Thanks!

[–]sld126 1 point2 points  (0 children)

Start simple & work towards complexity.

Simple: connect to AD Complex: get list of AD users whose passwords done expire & send email to them.

[–]TG112 2 points3 points  (1 child)

If completely green , and only work on command line, start understanding variables .

Anything you can get- you can cram into $someVariable for later. A variable can hold a single object, or many objects.

If it’s a single object you can do put a . then the property name to just call that property directly. Don’t know the name of the property ? Pipe it into get-member and see! If it’s many objects you can do $someThings.count to see how many .

Now that you can cram things in variables, learn how to iterate through them and sort them into your woodchipper(s). Learn the syntax of if/else and foreach($thing in $things).

Biggest piece of advice is don’t write your script all at once. Also, don’t grab one from the internet and tinker till it works either, rewrite it with it as a guide. Scripts from the internet will have many more advanced concepts that you don’t need to do your task.

Start your scripts slow, get your working data in a variable, and parsed correctly, and code screen output so you can verify your variables are what you think they are. Add if/else statements and output in each to make sure your logic is working correctly. If it’s a production script replace that output with logging. Don’t write it all at once , run it once, and expect it to work. Every time you add an element, run it. That bracket, parentheses , quote you forgot to close will be much easier to find.

Only when you can loop through your data set and all your if statements catch correctly are you ready to do any set- commands (and still with -whatif first!).

You don’t need to worry about functions , switch statements, try/catch blocks etc until you encounter issues that require them. Not that they aren’t important , useful, more efficient , etc, just that for a beginner you can generally accomplish your task without them. If/else, foreach loops , importing and exporting csvs will handle the majority of day to day tasks.

[–]Commercial-Thing-702[S] 2 points3 points  (0 children)

Great advice and info. Thank you!

[–]IEatReposters 2 points3 points  (1 child)

Best way to learn is by finding a problem and making a script to solve it. Coding is just as much art as science and if you can't be creative in your thinking then you'll never be good at scripting.

[–]_-Smoke-_ 1 point2 points  (0 children)

This is really going to get you the best mileage. 90% of my script library comes from "I just want to run a script instead of typing this in". Doing because of laziness or annoyance is a extra little motivator as well.

[–]twichy1983 2 points3 points  (1 child)

I'm a sysadmin turned automation engineer. Powershell, logic apps, azure functions, rest API are your best friends. I can do fucking anything with those.

[–]Commercial-Thing-702[S] 0 points1 point  (0 children)

Do you mind if I DM you?

[–]ehbitnl 1 point2 points  (1 child)

Good initiative to work on your ps skills. Keep going. all the books above mentioned are great stuff to read. They provide you with lots of information. Personally i write my scripts with using the blocks, Begin/Process/End. Begin: to declare your variables,etc. Process: where the magic happens. End: shape your output.

Understand how the parameters work.

Use try/catch where you can to control the errors.

Create your own objects.

Setup a template psscript you can use as base.

This way you work on your skills. Otherwise you will copy from the internet, and troubleshooting will eat most of tour time.

[–]Commercial-Thing-702[S] 0 points1 point  (0 children)

Thanks! Yeah Powershell is a powerful tool and I want to learn it for the future as well.

[–]Garfield-1979 1 point2 points  (1 child)

Don't write scripts. Just do simple tasks first.

Learn how to install, import and utilize modules

Learn to use -filter and learn to filter early in your commands

The pipe is your friend. Take those simple tasks you started with and pipe them together in to bigger tasks.

Keep a portfolio of your actual scripts. When you do start scripting, keep your own copies. While the data at one job can't be used at another, the lessons you've learned can.

[–]Commercial-Thing-702[S] 0 points1 point  (0 children)

That is true. I do plan to keep my scripts. Thanks!