PowerShell for Newbies

As you might imagine I get a lot of questions about how to get started with PowerShell, especially what books and material they can get their hands on. While I earn living creating books, training videos and offering live training, I’m aware that many people are on a budget so if you are looking for a free resource, allow me to direct you to an ebook/white paper I wrote for Veeam last year titled PowerShell for Newbies: Getting Started with PowerShell 4.0.

This is a 30 page crash course on everything I think you need to get started with PowerShell. There’s no way you can learn PowerShell in 30 pages, but this should get you started on the right path. I’d love for you to get a copy and let me know what you think. Head over here to grab your copy. You will need to register to download the paper. When you are finished come back here and take a look at my Books and Training page for your next steps.


Friday the 13th Fun

friday13 It is that time of year again. But instead of being freaked out by Friday the 13th, let’s have a little fun. Here is a collection of PowerShell one-liners all celebrating 13. And maybe you’ll even pick up something new about PowerShell.

Change is in the air

Petri_162x87If you have read my blog for any length of time, or followed me, then you know that I have contributed a lot of content to a number of online resources over the years, often on a recurring basis. You may not realize it, but spreading out my workload over several sites can be quite demanding. After a lot of deliberation, I’ve decided to consolidate all of my online writing efforts with the Petri IT Knowledgebase.

The Petri IT Knowledgebase has been around since 1999 and has always been a source of high quality content for IT Pros.  For the longest time it had a co.il domain name. But now you can find it at Petri.com. I will be publishing all of my online content exclusively at Petri where I will be a contributing editor. The site is being re-launched, lots of exciting things are in the works and I’m looking forward to being a part of it. At Petri, I will be primarily responsible for PowerShell related content. But you can expect to see me write about Active Directory, Group Policy, WMI, Hyper-V and Windows Server 2012 (and later).

I will still be blogging here and I will continue to create courses at Pluralsight. You will still find me at conferences like TechEd (or whatever it gets renamed) and the PowerShell Summit. I’ll still be doing PowerShell-related consulting and training on my own as well. Naturally, all of my existing content “in the wild” will remain where it is.

In most regards, nothing really changes other than the fact that you only have one place to check for my latest work. I hope you’ll come along for a great ride.

Pimp your Prompt

bling2If you are like me and live in PowerShell, then you spend a great deal of your day looking at your PowerShell prompt. That little indicator in the console and ISE that usually shows where you are. That little part of your PowerShell world is defined by a built-in function called Prompt. You can easily see the function like this:

This prompt is from PowerShell v4 but I’m pretty sure it is the same function that was used in v3. PowerShell v2 has a different function.

Did you notice that the newer function has a help link? Try it:

help prompt -online

You’ll get the online version of the about_prompts help topic. The great thing about the prompt function is that you can change it. I’ve posted a variety of prompts over the years. But here are 4 more for you to try out. These prompts should work in v3 and later. Most of the functions are simple additions to the standard prompt and should work for both the console and ISE. To try out the prompt you can paste the function into your PowerShell session. To make it “permanent”, insert it into your PowerShell profile script.

Include PowerShell Version

This prompt inserts the PowerShell major version into your prompt.

Include Admin

This prompt will test if you are running as Admin and if so, it inserts [ADMIN] in red text.

Include Computername

Do you like how a remoting session shows you the computer you are connected to? Why not have that all the time? All I’ve done is insert the local computername from the Computername environmental variable.


Auto Export Command History

This last version serves up a twist on transcription. When you run a transcript you get the command and results. But maybe all you want is a record of all the commands you ran. Sure, you could export command history at the end of your session, but you have to remember to do so and if you exceed your maximum history count, you’ll miss commands. In this prompt, everytime you hit enter, it gets the last command you ran and appends it to a log file. The log file is created in your PowerShell directory and uses the naming format of the PowerShell host, without spaces, a time stamp (YearMonthDay) and the process ID of the current PowerShell session. This allows you to keep multiple PowerShell sessions with separate logs. The log file will only record the command if it is different than the last one you ran. This also allows you to hit Enter without doing anything and not fill up your log.

If you temporarily paste in one of these Prompt functions, but don’t like it, you can simply restart PowerShell to get your original prompt. Or you can use this function to restore it.

This is handy to put into your PowerShell profile if you are experimenting with prompts. The Restore-Prompt simply defines a new Prompt function in the global scope. I’m using the default PowerShell prompt but you change it to whatever you wanted.

If you are doing something cool with your prompt, I hope you’ll share.

More PowerShell Laziness

lightbulb-ideaA few days ago I posted an article on using Update-TypeData to provide shortcuts to object properties. These shortcuts might save a few keystrokes typing, especially if you use tab completion. They can also give you more meaningful output. But you can take this even further and save yourself even more typing. How many of you have struggled to type an expression like this:

If you read the last article, you already know that I can use my alias property shortcuts. But you can also define other types of properties. Let’s say I often want to get file sizes in KB, and it is pretty tedious having to use Select-Object all the time with a custom hashtable. Instead I can do this with Update-TypeData:

When defining a ScriptProperty, the $this variable is used instead of $_. Using my previously created aliases, my command is now much easier to write:

With the aliases I can use them anywhere in a PowerShell expression. Let me leave you with one more example:

I often need to get the total value of something, but often I need it in a different format such as MB or GB. Now I have it.

That definitely saves some typing. I should probably do something similar for Maximum and Minimum properties as well.

So, what are you constantly typing? Is it something you could be smarter about? I hope you’ll share so I can take advantage of your laziness, I mean, efficiency!