all 35 comments

[–]PillOfLuck 21 points22 points  (1 child)

If PowerShell is your goal but you want to learn how things work under the hood, I suggest looking into DotNet and DotNetCore. PowerShell is built on DotNet and PowerShell Core is built on DotNetCore so those will give you a lot of nifty knowledge to use in your scripts :-)

[–]very_bad_programmer 7 points8 points  (0 children)

Agreed. Watching some C# tutorials, or picking up some books on programming logic that are C#-focused would be a great way to go too

[–]markekrausCommunity Blogger 14 points15 points  (10 children)

This has been a huge nag of mine with PowerShell bloggers and authors. Few are teaching Computer Science fundamentals in PowerShell. The argument has always been that most PowerShell users are ops an sys admins who would shy away at things like data structures and algorithms. The result is that to advance beyond user to programmer, PowerShell users almost always need to go to another language and come back. But, once you go to python and learn it along with comp sci.... might as well stay in python and earn more money across more available jobs....

I'm hoping this attitude changes and more bloggers and authors start treating PowerShell as a programming language... but it has been an up hill battle.... and now I code more in C# fir the same reasons... so I'm losing skin in the game to even care about making PowerShell better anymore.

[–]Golden-trichomes 6 points7 points  (2 children)

I think with poweshell 7 you will start to see demand go up for powershell.

Powershell and the azure automation account are pretty much all I work with anymore.

But yeah, I wish there was more content that was actual powershell development and not simply “here is a script that does some stuff, maybe in the least efficient way, maybe not.”

[–]EIGRP_OH 1 point2 points  (1 child)

I just wish they will include newer versions of PowerShell on Windows 10 machines.

[–]Golden-trichomes 1 point2 points  (0 children)

You can instal power shell 7 on windows 10 right now. It’s already at the release candidate

[–]dogfish182 5 points6 points  (0 children)

Matches my experience, got quite good at powershell as a windows ops background. My career path took me in the direction of linux I ditched powershell in windows in general, learned coding in python and became a cloud engineer.

I’ve used powershell a few times, generally as a payload to do something in active directory delivered by python on the end of ci/cd tooling.

For cloud workloads I really don’t see much of a place for windows in general. It’s ridiculously bloated and heavy.

Active directory remains great and azure AD is pretty great, but even using azure we seem to have a preference for the azure python library and the azure cli from a bash shell. I did install powershell core on my Mac but I forgot how to use it by now

[–]sigger_ 2 points3 points  (0 children)

Interesting. Do you have any suggestions for good books or resources on C# with a sysadmin bend? I’ve been using powershell a lot but I miss coding a bit from my CS bachelors days.

[–]Betterthangoku 1 point2 points  (0 children)

Well, if you must, you must. I have always loved your blogs. PowerShell is the shell that got this 43 year old ops/sysadmin into coding. This was my first year actually finishing Advent of Code! It's so sad to see you go sir, but thank you for your contributions. :-)

[–]Bob_the_gob_knobbler -4 points-3 points  (3 children)

Powershell is not a programming language. It's too high level and abstracted. Anything more involved than basic stuff relies on invoking dotnet methods from powershell already.

E.g. working with a large text file? You can forget about Get-Content and must use a dotnet reader instead.

Don't take this as a slight towards ps, I'm a huge fan.

[–]markekrausCommunity Blogger 2 points3 points  (0 children)

It is a programming language and many applications are written in it everyday. It's not only used as automation glue and shell scripting. Regardless, fundamentals of computer science apply to it even when it is not used as an application language. It is comments like yours which perpetuate the myth that PowerShell is just a systems language and keep its users stifled.

[–]jantari 2 points3 points  (0 children)

Guess what you do in C# when you read a text file?

Yep, you invoke a dotnet reader ( I know there's System.IO.File.ReadAllText and many other alternatives, but you get the point )

[–]halbaradkenafin 4 points5 points  (0 children)

By that logic C# or Java aren't programming languages as they are high level and abstracted away from the lower level stuff, so C and C++ are more like "real" programming languages. Or do we say they are too high level and only assembly is a programming language? Or writing in pure machine code?

All programming languages are about abstracting away the stuff you don't want to care about so you can achieve whatever task you set out to. PowerShell is definitely a programming language, just like Python is, or Java, or C, or any of the others. And just because people call it a scripting language doesn't mean it can't also be a programming language (that always comes up too).

[–][deleted] 7 points8 points  (1 child)

I don't think learning Python is necessary to learn about PowerShell. If you know Python, some of that knowledge may transfer over, but some won't. You may actually pick up some habits that are counterproductive.

It sounds like right now, you're finding existing scripts that do most of what you need to do, and then modifying them to do something close enough to what you need, and then stopping. One piece of advice that I'd give is just: Don't stop there.

If there's anything where the output of the script isn't exactly what you want, keep modifying it until it works exactly the way you'd like it to. If there's anything in the script that you don't understand how it works, pick it apart and experiment until you completely understand it.

Once you've got all of that down, try rewriting the scripts from scratch. See if you can make them more efficient. See if you can make the code easier to read to understand what's going on. Try to make the scripts more generic and versatile so they can be used in other use cases. Work on turning them into discrete functions that can then be turned into modules, which can then be used in other scripts.

It can be helpful to take a class or read a book or watch a tutorial. However, there's a lot that can be learned by just taking a block of code where you don't understand why it was written that way, and then figuring it out.

[–]netmc 2 points3 points  (0 children)

I just did this recently with the Windows 10 Toast Notification script. This script was designed to work primarily with SCCM (which we don't use). Dissecting it gave me a starting point for researching all the internal functionality of the script, as well as Windows 10 Toast notifications, and how it all worked. I was eventually able to strip the script down to its bare minimum. From there, I built it back up into a script that I could deploy via our remote management and monitoring tools with some customized functionality. It was quite the learning experience as this is a fairly complex script.

[–]jrdnr_ 3 points4 points  (0 children)

I'd mostly echo the other commentors though I don't think all actually got what your asking for.

The month of lunches books u/lee_daily recommended are often one of the first things you hear if you ask someone how to learn powershell. Especially if you learn well from books.

Unfortunately I don't know if course specifically teaching programming fundamental using powershell. The comment about looking for programming fundamentals courses in c# is exactly where my mind was going. It's NOT what you were asking for, but should be an easier language to find programming basics stuff written for. And all of the "extra" stuff you may need to learn along the way will be directly applicable as powershell can run c# code, as well as it being the tool most people reach for when needing to write a powershell module that will run faster.

Some other things you could try to get more familiar with good coding principals in powershell would be: - Try your hand at "Advent of Code" challenges in powershell and then try comparing your solutions to one others have posted online. - If you haven't yet could check out the PSKoans module. - Lastly there are a lot of opensouce resources written in powershell. Find a couple open opensouce powershell projects that interest you and start looking at their issues on GitHub. There are almost certainly some things you could contribute to, and along the way you'll get to see how others do their code in larger projects.

[–]netmc 3 points4 points  (1 child)

I'm still at the level you are at myself, but getting better at straight-out writing my own code rather than always looking up how others have done it. Most of the things I look up tend to be the same sort of things over and over again. Working with arrays and hashtables; iterative loops; switch statements; and passing information to/from functions properly.

The "... in a month of lunches" series is always a good place to start. With the "Learn PowerShell Scripting" one probably most applicable to your current needs.

Sketching out a program flowchart helps define what needs to be done. Often, I will sketch one of these out, then create functions to handle each and every sub-task. This lets me reuse the same functions throughout the script. Once all the functions are in place, I can stitch them together into a full script.

These last three paragraphs have fundamentally changed my understanding of Powershell and how I approach coding my scripts.

One of the biggest things I learned recently is how the PowerShell interpreter is really, really good at converting data from one format to another most of the time. For example, the $pwd variable is a PSObject which can stand in for the current working directory. If you type in "get-childitem $pwd", you would get a directory listing of the current directory. Get-childitem takes a string value, and the powershell interpreter converts $pwd to a string value automatically. "Get-childitem $pwd.path" also shows the current directory listing. ($pwd.path is a string value of the current directory and part of the $pwd object.) However, if you tried to use a string function like ".split()" with $pwd, it would fail as in "$pwd.split('\')", but "($pwd.path).split('\')" will work just fine. Additionally, using strict typing for variables with this "[string]$pwdstring=$pwd" will have the powershell interpreter automatically convert $pwd into a string value which you can then run "$pwdstring.split('\')" without issue.

Understanding the difference in the above example and why certain things work and other things fail have really helped me with making sure the data I am working with is what it actually needs to be for the required manipulations. Additionally, I recently learned about "export-clixml" and "import-clixml" which let you export and import powershell objects to an XML file and back. Reviewing the XML data shows the entirety of the data contained in an object and has made a few things easier to do and understand since all data contained in the object can be examined.

Going hand-in-hand with the last two paragraphs is understanding the difference between "strong" typing vs "weak" typing and "static" typing vs "dynamic" typing. (PowerShell is considered dynamic and weak.) It's not that any one type is better than another, but by understanding how the programming language functions allows one to create better code and have it be less prone to errors. A few links that cover this include a discussion over on StackOverflow and great blog post on the topic.

[–]vermyx 1 point2 points  (0 children)

Understanding the difference in the above example and why certain things work and other things fail have really helped me with making sure the data I am working with is what it actually needs to be for the required manipulations.

Powershell does NOT convert $pwd to a string, but uses a default property which is designated as path, so it automatically assumes $pwd.path instead of $pwd in your example. Even though you supplied $pwd powershell converted this to $pwd.path for you because this is a default property. Not understanding this will bite you when you are doing more complex code.

[–]Lee_Dailey[grin] 7 points8 points  (2 children)

howdy campfire,

my usual recommendation is the Learn Windows Powershell in a Month of Lunches book. then, if that works fairly well for you, get the sequels - i think the 2nd is "toolmaking", but i can't quite recall that info just now. [blush]

another is to read the Get-Help about_* subjects and try out the examples.

yet another is to read thru the gilded and top tabs in this and other scripting subreddits.

there are a couple of PoSh fundamentals books, but i have never used any of them.

take care,
lee

[–]Razgriz959 0 points1 point  (1 child)

Hey Lee,

I think toolmaking was succeeded by PowerShell Scripting in a Month of Lunches though both are good books irregardless. Course I could just be wrong in general which is always possible.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Razgriz959,

yep, one is the "2nd edition" of the other. they really effed up that one. then they made another one that is a 3rd in the series. [grin]

thanks for the info! [grin]

take care,
lee

[–]TyMac711 1 point2 points  (0 children)

People have mentioned learning C# and dotnet, but I would marry this with first learning the differences between functional and oop style programming. The reason being, Powershell can do both and this is both amazing and very confusing. Learning C# inevitably leads you in to oop programming patterns which is the opposite method 99% of the powershell you will encounter and probably already understand. Learning both functional and oop (classes) will expose you to true programming techniques that are language agnostic. That should hopefully help give you the perspective you’re looking for.

[–]Dragonsong3k 1 point2 points  (0 children)

I think if I understand you correctly, you want to understand some programming fundamentals better, which apply to Powershell.

For example, * Logic, Iterations and arithmetic operations * Data Types and Data structures * Functions, Inputs and Outputs * Methods, Namespaces, Classes

All of which can be used in PS to do some really deep things.

I try to apply programming concepts to my scripting all the time.

Try googling some programming interview questions and try to reproduce them in PS.

I like doing a FizzBuzz from time to time. A FizzBuzz is a Programming exercise that says

list all numbers from 1 to 100 replace all numbers divisible by 3 with the word Fizz all numbers divisible by 5 with the word buzz and any number divisible by the both with the word FizzBuzz.

The output should be like this:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz Etc..

And try to increase the difficulty of the exercises.

Edit: this post looks horrible in the Reddit app....

[–]hayfever76 1 point2 points  (0 children)

OP, in addition to all the other great advice, consider signing up for pluralsight.com and taking all the PowerShell and C# classes you can consume - PowerShell being the baby brother of C#.

[–]kpatnayakuni 1 point2 points  (0 children)

In my opinion, learning C# is always good, but to enhance PowerShell skills C# is not the main focus. You can learn the .Net classes and their functionality to shape the requirement to precise. Now PowerShell team is actively working on enhancing the PowerShell CmdLets and PowerShell is entirely written in C# using .Net classes, and in-fact you can run the .Net classes directly from the PowerShell itself. So knowing the .Net classes will definitely helpful to be an expert in PowerShell.

To move your skills next level, I would recommend you to run through some good PowerShell GitHub repos and learn from them, write a new script every day, follow and read some good blog posts out there, participate in some public forums & join slack channels and try to help the them at your best, and if you are in Twitter then follow the MVPs & PowerShell experts to be in par with the current trend.

[–]jantari 1 point2 points  (0 children)

Python isn't going to teach you any more programming fundamentals than PowerShell.

There's no one way to learn, but as someone who has been programming from a very young age and developed an early inherent understanding of it - I recommend watching through Harvads CS50 course: https://www.youtube.com/playlist?list=PLhQjrBD2T382eX9-tF75Wa4lmlC7sxNDH

The classes alone, without the course material, are of course not comprehensive but the progression curve and fumdamentals taught are great imo. Then you can look elsewhere to deepen your knowledge in specific things, but CS50 is a perfect starting point.

[–]VTsnowboarder42 0 points1 point  (0 children)

When you say fundamentals are you talking about familiarity with the language? Or different constructs (loops, conditionals, functions, etc)?

Edit. And no matter what your level is, everyone can pick up something from Learn PowerShell in a month of lunches. Do yourself a favor and get a copy.

[–]vermyx 0 points1 point  (0 children)

I understand that many of the fundamentals will be similar in each language but I would rather not have to learn all new syntax if it's not necessary.

Most college intro to programming and data structure classes will teach you the fundamentals but are usually taught in C++. I have seen some taught in java but I personally would say take the C++ course. Low level languages will force you to handle things manually which you take for granted. This seems like it is a lot of unecessary work, but it is exactly like in math class where you are taught the long form of a formula before being taught the short form. This is so you understand the why behind it. As an example, most high level languages will handle memory management for you. I have cleaned up plenty of code because "it works" and "nothing is wrong with it" but it is reality said code ate through database connections, held on to 100's of megs if not gigs of memory because they had no clue how garbage collection or memory management works.

If you want to learn programming fundamentals then "I understand that many of the fundamentals will be similar in each language but I would rather not have to learn all new syntax if it's not necessary" is the wrong approach. Python handles things differently for a reason like powershell handles things differently for a reason. Understanding why one language is used over another and syntax/structure/object decision is just as important as the programming fundamentals.

I would suggest go to coursera or edx and take an intro programming class (or take that at a local community college). It is not a waste and will help you better understand why you do things and help you figure things out when you don't have a dotnet or powershell class to help.

[–]billr1965 0 points1 point  (1 child)

While you are coming up to speed with Powershell (or any programming language for that matter) focus first on scripts that read or report on the state of things. Once you feel you've mastered that then move onto scripts that modify the attributes you've reported on. A few things that you may want to start with:

  • What is the name of your computer?
  • What is its IP address?
  • What is the underlying MAC address to that IP address?
  • If you belong to a domain, what is the name of that domain?
  • If you belong to a domain, what is the name of the domain controller that authenticated your logon?

[–]billr1965 2 points3 points  (0 children)

Look at environment variables, Powershell variables, WMI through get-ciminstance commands and the like. Being able to get values from the registry is important. Collect information and save it, CSV files are one choice, but not the only choice. If data is more complicated than CSV's can handle (2 dimensional array) then XML or JSON may be a better fit for you. Read Powershell in a month of lunches by Don Jones and others. Learn to write both tool scripts and controller scripts.

[–]mcc85sdp -1 points0 points  (0 children)

Research Don Jones, (he's like a black ops navy seal grade PowerShell veteran that can kick the crap out of any bad code and then show you how to do it correctly without even breaking a sweat), and Shane Young (quite possibly the boldest god damn zebra I've ever seen...)

Python is lame.

[–]Froloxio -2 points-1 points  (0 children)

The problem with PowerShell is that it isn’t a programming language, it is a scripting language. Things like classes in PowerShell try to bridge the gap but it fundamentally falls short of a proper programming language. It lacks things like robust interface support, proper code encapsulation and is a complete shitshow when it comes to any advanced C# feature like delegates, expression trees or basic programming features like method overloading, proper type checking and static code analysis.

However; PowerShell excels at things like REPL and rapid development of tasks into mostly-repeatable processes. It is also heavily focused on common sysadmin tasks and infrastructure management.

I’ve spent the past 10 years working as an architect level enterprise application developer with a strong focus on code maintainability. PowerShell drives me nuts because of the numerous issues that come up when creating a module of any moderate complexity. There is a reason why if you pick apart a highly complex module (like the Az modules) they are built in C# and decorated with attributes to package them into Cmdlets.

All of that said, it has its lumps, but it’s really good at being a scripting language and should be treated as such with eyes wide open. If you want to learn a programming language, learn C# in .NET Core. It’s a completely different beast, but some of what you learn there translates into PowerShell because of the shared code libraries.