To Script or Not To Script? That is the Question We Need to Answer

This blog post was created from the transcript of my YouTube #CarTalks video called To Script or Not To Script? That is the Question. Be check out the YouTube channel and subscribe! When to script and when not to script is our topic for today. I know myself and my audience; we’re all about scripting. We want to write PowerShell scripts for everything, but, unfortunately, it’s not always the best decision to do so. I know it sucks, but I’m here to hold your hand and tell you, “It’s okay! We’ll get past it.” Know Your Coworkers For example, you’re the PowerShell scripter guru extraordinaire at your small organization. You work with a handful of people that are in the same realm – helpdesk guys, other sysadmins, and network engineers. Say you build a script that is keeping some service alive on a server, e.g., some critical business service that […]

Read more

Start-Sleep: The Simple yet Underrated PowerShell Cmdlet

The PowerShell Start-Sleep cmdlet or the sleep alias is a simple cmdlet with a single purpose; to pause a script. When executed, in the PowerShell console, a script executed by the console or in the PowerShell ISE, the cmdlet pauses merely a script or module in the PowerShell session from running until the required time in seconds or milliseconds have elapsed. This cmdlet is simple yet can be applied in a few different ways that will allow us scripters to greate well-written scripts. Start-Sleep Usage Using the Start-Sleep cmdlet is extremely easy since, after all, it only has two parameters! Let’s say I want to pause my script because I’m waiting for some other environmental process to run. That process takes around 10 seconds, and I need to be sure that my script doesn’t keep running before that external event is done. To pause the script for 10 seconds, I’d […]

Read more

Invoke-Command: The Best Way to Run Remote Code

IT professionals rarely work just on our local computer. Using the PowerShell Invoke-Command cmdlet, we don’t have to! This cmdlet allows us to seamlessly write code as if we were working on our local computer. By using the PowerShell Remoting feature, The Invoke-Command cmdlet is a commonly used PowerShell cmdlet that allows the user to execute code inside of a PSSession. This PSSession can either be one created previously with the New-PSSession cmdlet or it can quickly create and tear down a temporary session as well. Think of Invoke-Command as the PowerShell psexec. Though they are implemented differently, the concept is the same. Take a bit code or command and run it “locally” on the remote computer. For Invoke-Command to work though, you must have PowerShell Remoting enabled and available on the remote computer. By default, all Windows Server 2012 R2 or later machines do have it enabled along with […]

Read more

The FREE 2017 #PSBlogWeek eBook is Here!

2017’s #PSBlogWeek is long over and as a tradition, I’ve created an eBook commemorating this great event. Thanks to all of the bloggers that wrote some awesome posts and I hope we can do this again soon! As promised, the 2017 #PSBlogWeek eBook is completed and now available. Feel free to put it up on your blog, tweet about it, share it on Facebook or anywhere where you think people will enjoy it. All I ask if you link back to the original blog post announcing it here.

Read more

Test-Connection: Ping Remote Hosts the PowerShell Way

Today’s cmdlet of the day is Test-Connection. Test-Connection is a cmdlet that not surprisingly tests your network connection. Think of Test-Connection as PowerShell’s implementation of the popular ping utility. Even though both have ICMP in common, you’ll see that the two methods are a little different under the covers. Using this cmdlet is simple. At it’s most basic, just specify a ComputerName parameter, and it will send four ICMP requests to the destination host.

This output looks similar to ping.exe and, on the surface, it is but Test-Connection issues the ICMP request a little differently. Unlike ping.exe, Test-Connection is using the local computer’s WMI class Win32_PingStatus to send the ICMP request. Using the local WMI repository means you’d better be sure your local WMI repository is healthy else Test-Connection will not work. Test-Connection’s Object Output Also, as with the beauty of PowerShell, this cmdlet doesn’t merely return what immediately […]

Read more

Set-Content: The PowerShell Way to Write to a File

Set-Content is one of those core PowerShell cmdlets that I can’t do without. I still remember using VBscript before we could use PowerShell to write to a file. I remember always trying to remember what kind of object I needed to use and the method name. Was it FileSystemObject, FileObject or what? It was a pain! Also, even when I did recall the method name was CreateTextFile, I’d always forget to add True as the second argument. Here’s an example of the monstrosity I’m talking about.

Compare that VBScript with this PowerShell:

Which one do you prefer? I’ll take the PowerShell way! The PowerShell way uses a single cmdlet called Set-Content. This cmdlet allows us to much more easily use PowerShell to write to a file. This PowerShell cmdlet is a built-in cmdlet that has one purpose; to write to a file. It may have some parameters here […]

Read more

Set-ExecutionPolicy: Your Key to Opening PowerShell Scripts

Set-ExecutionPolicy is our next PowerShell cmdlet of the day! This cmdlet is one that most newcomers to PowerShell get familiar with real quick especially if they launch PowerShell on an older operating system. Why? Because PowerShell script execution and any PowerShell module wouldn’t work! A newbie may create a script in the PowerShell ISE, try to execute a PowerShell script and immediately wonder why her script won’t execute. They’d then do a little Googling and find the method to find the applied executio policy, run Get-ExecutionPolicy to find it is restricted. One of the first hurdles everyone new to PowerShell must overcome is figuring out how to just run scripts. Before Windows Server 2012 R2, it was not possible to run any scripts without first opening a PowerShell window and running the PowerShell Set-ExecutionPolicy cmdlet in the PowerShell console. Nowadays, the restrictions have been lifted a little bit but it’s still […]

Read more

Get-AdUser: A Cmdlet a Day

Get-AdUser is our first PowerShell cmdlet for the day! If you’re in IT, chances are you’ve got some implementation of Microsoft’s Active Directory (AD). A huge part of AD is users. Users are what gets assigned to individual employees, service accounts, etc. Get-AdUser is a cmdlet inside of the freely available Remote Server Administration Tools (RSAT) package. Once you have the ActiveDirectory module installed, let’s take a look at the cmdlet we’re here for today! Get-AdUser has one purpose and one purpose only: to give you as many options as possible to either find a specific user object where you already know information about or return lots of users that match specific criteria. Finding an Individual User Account with Get-AdUser If you already know a couple different criteria for a user object already Get-AdUser allows you to find information about a single user. To do so requires knowing one of […]

Read more

Introducing the Cmdlet a Day Series

I’d like to go back to my roots a bit for a series of posts to help the PowerShell newcomer or dust off some of those old cmdlets that you may have forgotten about. I’m starting a series I’m calling a Cmdlet a Day that goes over, in detail, one specific PowerShell cmdlet. These cmdlets can be a default PowerShell cmdlet or could be part of a popular PowerShell module as well. The main point of this series is to help newbies learn PowerShell. I hope it will also spark some ideas for all of you experts out there or perhaps teach everyone a little bit more about a cmdlet you either didn’t realize existed or never took the time to learn it. When I first started, I’d see all of these hundreds (now thousands) of cmdlets available to me and would get overwhelmed. I never knew the right one […]

Read more

The Two Step Guide to Upgrading to PowerShell 5.1

PowerShell 5.1 has been out now for nearly a year, but I’ve found that many companies have yet to upgrade. Perhaps the reason they haven’t updated is that they’re at PowerShell 5.0 and don’t see the need to upgrade or they’ve built code in the v2 days where they feel like something may break. If you do decide to upgrade, I’ve created a technical how-to with some scripts to automate this process for you. Disclaimer: All code mentioned in this post is as-is. Some may work, some may not work in your environment but, if you have nothing, to begin with, it will act as a template for you to start with. Since I’d just be repeating what sites like like the PowerShell 5.1 release notes and the installation help that Microsoft provides for 5.1, this isn’t going to be about 5.1’s features but rather a tactical how-to on getting […]

Read more
1 2 3 8