Q&A
Don't Be Afraid of PowerShell
It's not as complicated as you think. Get automating today!
We get it. From the outside, PowerShell can appear to be an incredibly powerful tool that only the most hardened IT pros can wield. But that just is not the case.
Petri Paavola and his 20-plus years of Microsoft environment experience has made it his mission to get IT to be more effective and save time by jumping in to the PowerShell pool. And to prove to you that it's not as deep as you would expect.
Ahead of his demo-heavy session (where he will show that any level of PowerShell user can take immediate advantage of automation) at next month's Live! 360, Paavola sits down with Redmond to discuss some of the misconceptions of PowerShell, how you can get up and running with the tool today and what impact Gen AI has on Microsoft's automation program.
And don't forget to attend Paavola's session, "How to Use PowerShell to Become a Windows and Cloud Management Superhero - 2023 Edition" in Orlando, Fla. next month. (This is your last week for early-bird registration!)
Redmond: Do you think IT pros broadly think PowerShell is more complex (and complicated) than it really is? If so, why?
Paavola: Yes I think this is one problem. Even in longer scripts there can be only few lines which actually does something meaningful.
I have created scripts that are one line in length, but generate a script that is 100 to 200 lines long. All other lines are preparation and checking that we can run that most important command. So skill to find the most important commands from PowerShell script helps a lot to save time.
Most of the PowerShell scripts are repeating same logics all the time: Get -> Filter -> Foreach.
I have done scripts which are over 5000 lines of code. Yes they are looong scripts but in the end what they do is: Get data -> Filter data -> Process data in Foreach loop and this is repeated many times. I would say that most of the PowerShell scripts in the world follows the same logics.
Sometimes it helps to oversimplify the code to understand what code is doing in big picture. Then you can drill down code level to see how script is created.
PowerShell also has really easy syntax. I once wrote 300 lines of PowerShell code before noticing that I actually hadn't run the code yet. 300 lines is a lot to debug if something doesn't work. Well, the 300 lines of code I wrote worked because once you understand the PowerShell syntax, it's difficult to make a mistake.
What's the most common mistake that IT pros make when it comes to PowerShell? Bonus points if it's something that's completely avoidable, or based on a misconception, etc.
What you see is not what you have. In PowerShell we usually have more data than is shown in PowerShell console. In short, this line will show you more information (if there is any):
$variable | fl *
Another typical mistake is not understanding PowerShell pipeline and objects. This is THE thing which differentiates PowerShell from other programming languages. Yes, pipeline and objects are available with other programming languages too. But with PowerShell, it is so easy when you understand what really is happening. Also PowerShell syntax is really easy and the code itself explains what it is doing. Your PowerShell skills will go to the next level the day you understand PowerShell pipeline, objects, filtering and processing of objects.
So first you need to understand what data you have and then you need to know how to filter, search, use and manipulate the data.
PowerShell's most popular uses cases -- automation, remote management, monitoring, etc. -- are well known. What are some use cases that you think IT should be looking at more?
PowerShell has really good tools for data conversions, which could be used in integration workflows. Some conversions have built-in cmdlets which just do the work: CSV, JSON, XML and HTML just to name few. We can easily create our own data structures, which we can convert to desired target format really easily. Although in high volume data, PowerShell can struggle. But there are ways to create efficient code with PowerShell to make up for that shortcoming. One of my scripts would have run over seven days at the initial onset. After optimization it ran in five minutes. This requires knowledge of data structures optimization (hint: Hashtables are FAST).
PowerShell provides tools to create HTML reports really easily from any data. And with some creative HTML CSS and Javascript code (which you get from ChatGPT), you can create really good looking and responsive HTML reports. Or use some third-party PowerShell module for HTML creation which has same capabilities.
I have used a lot of time to create amazing community tools for Intune management. My HTML reports show information which is not available in Intune portal. And with PowerShell we can even create fully working Windows application UIs. Although that route can go really deep really fast!
In Microsoft's cloud world, scripts using Graph API are more and more common. It may sound a complicated task but actually it is really easy. Actually it is so easy that anyone can create Graph API PowerShell scripts. You just need to use my ClipboardTools-helpertools and you can write Graph API PowerShell code without even knowing how to do PowerShell Graph API code at all.
So, generative AI is the big elephant in a lot of room today. How do you think it's going to affect PowerShell? Are we seeing any of these effects right now?
Generative AI is a huge change! Depending on own role it could be seen as a threat but I see it is a huge opportunity! And it is here now!
There is a saying that generative AI will not replace you but a person using generative AI might replace you. I choose to be the person using these new tools and opportunities.
I use ChatGPT and Bing Enterprise Chat daily for many tasks. With PowerShell it can help in many ways. It can create PowerShell script structures when you know in pseudo code (pseudo code means write script logics with normal words to describe what you want to do) what you want to do and help with anything when you have basic knowledge what you are doing with PowerShell.
It can find patterns from text and help searching (regex) and manipulating pretty much any text formats. I have used it with more advanced JSON and XML manipulation. I can admit that with the PowerShell XML manipulation task, ChatGPT won me over. It provided working answer to my problem when I hadn't even figured myself how I would try to solve problem even with pseudo code.
And there are so many other examples. Wins and frustration but I'm still happy to use it every day.
And that's what everyone should be doing. Just use it. You'll see that it can help explain code, document existing code, create new code, fix existing code and create documentation pages for the code. It's not just PowerShell code itself but all things that relates to PowerShell coding.
Need to learn something new, then just tell ChatGPT to teach you. You can even tell it to create teaching material to selected topics.
Sometimes generative AI doesn't get it right and it can go wrong even if you tell it the right answer.
Many times you have to know how to code so you can effectively tell generative AI to create the content you want and need. And the more you know about PowerShell, the more you get out from generative AI. I many times have to tell it to do some task in certain way to get the results I'm looking for.
With generative AIs you have to also learn how to prompt it. The more you learn about prompting, the more you get out of it. AI prompting is a new skill you need to learn and you learn by doing more and more.