• 11 Jun 2012

    Using Hash Tables and Select-Object

    If you don't like what PowerShell commands return to you, send it on to Select-Object ! Select-Object is one of the most important cmdlets because it allows you to pick the columns (properties) you want to see. Check out these lines: PS> Dir $env:windir PS> Dir $env:windir | Select-Object -Property * PS> Dir $env:windir | Select-Object -Property FullName PS> Dir $env:windir | Select-Object -ExpandProperty...
    • 15 May 2012

    Test-Driving PowerShell v3: How To Install And Run PSv3 BETA

    A new PowerShell version is coming soon! The language itself will stay pretty much the same, but there are tons of cool new features that enhance performance and ease of use . A complete list of all of the new features is available here: http://www.microsoft.com/en-us/download/details.aspx?id=28998 (see the lower part of this page for feature details). Which raises the question: how do I install and get PSv3 up and...
    • 9 May 2012

    Managing Child Processes

    Today in my training, a student asked how to manage child processes . That's not a bad question at all, and as it turns out, it's a great way to show off the capabilities of PowerShell . So why would you care about child processes in the first place? Well, for example to monitor and keep track of what installation packages are doing. When you launch an application from powershell, like setup.exe for example...
    • 1 May 2012

    Don't Waste Speed - How To Activate The PowerShell Turbo Mode

    This afternoon, my buddy Alexandar (and fellow PowerShell MVP from Serbia) reviewed another batch of PowerTips when he stumbled across some lines of code that looked a bit unintuitive to him and asked to rephrase them. Sometimes there is more than one way to Rome. There is nothing bad about having choices. Today I'd like to show how to pick the fastest way to Rome - and how you can speed up your PowerShell code tremendously...
    • 23 Apr 2012

    Displaying Data in Excel (and why it won't work)

    It's pretty easy to send PowerShell results to Excel and display them nicely as a spreadsheet. Actually, here's a simple function called Out-ExcelReport . Pipe anything to it, and it shows up as an excel sheet (provided you have indeed installed excel beforehand): function Out-ExcelReport { param ( $path = "$env:temp\report$(Get-Date -format yyyyMMddHHmmss).csv" , [ switch ] $open ) $Input | Export...
    • 19 Apr 2012

    Finding Logged On User (and Writing To Other People's Registry)

    How would I find out the currently logged on user on a remote machine ? And how can I remotely access another person's HKEY_CURRENT_USER registry hive? Here are some tricks you may want to try... Who's Logged On? To find out who's logged on to another machine, you could use WMI and check for the owner of explorer.exe . Since explorer.exe is providing the GUI, by looking at its owner you find the logged...
    • 11 Apr 2012

    Shorten Path Names (and using Low Level API functions)

    Path names can grow large, and sometimes there is just not enough room to display the entire path. There is no cmdlet either that would shorten the path for you. So let's take a look today at how you can access low-level Windows API functions from PowerShell to add this feature, As an immediate benefit, after you read this article you can optimize your PowerShell prompt and make it shorter while keeping the current...
    • 2 Dec 2011

    Regular Expressions Are Your Friend (Part 3)

    In Part 1 , you lerned how Regular Expressions can extract useful information from noise text. In Part 2 , we looked at using Regular Expressions to split text, yet another powerful technique to extract the pieces of information you may need. Today, we conclude our little excurse and use Regular Expressions to replace text . Fasten your seat belts, please! Replacing Old Text With New Stuff Replacing some information...
    • 14 Nov 2011

    Regular Expressions Are Your Friend (Part 2)

    In Part 1 , we used Regular Expressions to extract useful information from noise text. That was pretty cool, and we used the operator -match , the automatic PowerShell variable $matches and the powerful Get-Matches function. Today, we'll use Regular Expressions again, but this time they are used to split text. Chopping Text Into Pieces Regular Expressions can be your butchers' knife to split text into...
    • 27 Oct 2011

    Regular Expressions Are Your Friend (Part 1)

    Yes, regular expressions can be complex but hey no, they do not need to be! You can do amazing things with small and relatively simple regular expressions. So let’s take a look at it. Finding Information: -match Often, you need some piece of information that is buried inside of noise text. Let’s assume you’d like to find the KB article number that is in this text: PS> $text = 'The problem...
    • 14 Oct 2011

    Reuse Your Code - Create Modules Automagically!

    When you write scripts, you want to automate things, fair enough. When it comes to writing scripts, a lot of people do not automate, though. They do the same coding over and over again and waste time and consistency. So let’s look at some simple code-reusing techniques. It’s not that hard at all. In fact, after you read this post you can create your own PowerShell modules in seconds. Code Reuse –...
    • 21 Sep 2011

    Applying NTFS Permissions

    Recently, I needed to create a folder with NTFS permissions . PowerShell can do that for you, and when you look at your weapons, you'll find that sometimes it is best to mix command types and not just stick to cmdlets. At the end, I had a handy function that would take a path and a username and do all the tricky NTFS permissions stuff for me. Here is how. Creating Folders Let's start with the simple part...
    • 12 Sep 2011

    Bulk-Renaming Files (and other magic)

    Bulk-renaming files can cause some headache: maybe you'd like to nicely rename all your pictures with a keyword and an incrementing counter, or you'd want to clean up log files and assign them a standard name based on its creation date. It's easy to do that for one file, and with our latest tip , we illustrated how you can as just as easily do this for hundreds of files, too. Heck, did we get a lot of feedback...
    • 3 Aug 2011

    Clever Splatting to Pass Optional Parameters

    Getting system information from WMI is a pretty straight-forward thing with Get-WMIObject . You can even specify one or more computer names or IP addresses to retrieve the information remotely. That's all great, but how would you design a function that encapsulates Get-WMIObject (or any other cmdlet that can work both locally and remotely) ? That's not so trivial. Have a look. There's a really clever solution...
    • 23 Jun 2011

    Dealing With File Locks

    Sometimes, when you try and access a file, it may refuse to open because it is locked by another user or process. Likewise, you may want to lock a file yourself to make sure the file is not accessed and read/changed while you are manipulating it. Which raises the question: how do you control file locks? Locking a file Get-Content can read text-based files. This cmdlet is not locking the file, so while you are reading...
    • 23 May 2011

    Renaming Files Puzzle - And Three Golden PowerShell Rules

    I am moderator for one of our Ask-the-Experts forums , and every once in a while, I get questions I would like to discuss. Like this one: "I am tring to write a quick interactive command that will rename all the jpgs in the current directory and came up with the following command: $i=1; gci | ? { ($_.Extension -eq ".JPG") -and !($_.Name.StartsWith("cover")) } | % { Rename-Item -Path "...
    • 17 May 2011

    Troubleshooting Windows (and Dimming Your Display, too)

    You can't get a device working you just hooked up to your computer? Your computer is running slow? Or you just can't get these fancy Aero Glass features working? When bad things happen, it's usually time to get some coffee, lock the door and browse zillions of Internet forums that provide more or less useful suggestions. Or is there a smarter way ? With Windows Vista and Server 2008, Microsoft introduced...
    • 28 Apr 2011

    Multiple Text Replacement Challenge

    Ready for a challenge? Here it is: take a text, and replace a number of different characters. How would you do that? I wanted PowerShell to replace all German special characters with alternative text. Boy, was I in for a surprise! If you'd like to take on the challenge, pause reading now and go find a good solution! Then come back and compare it with the solutions that came to my mind. Ok, if you do take...
    • 12 Apr 2011

    One PowerShell Tip A Day Keeps The Doctor Away

    You probably know I have been writing one PowerShell tip every day - since 2008. This piled up to a tremendous gold mine of little tips and tricks you can use to improve your code. Sometimes, the truly amazing thing is to see what you do with these bits and pieces, and today I'd like to thank Robbie Wiley for sharing his personal use case and how he redesigned it using one of our last weeks' tips. "...
    • 13 Mar 2011

    Executing Code On Behalf Of Someone Else

    In almost all of my trainings I get this question: "Can I launch a program remotely so another user can actually see it?" To answer that question, I'd like to take you on a quick security tour and provide you with a test tool that may or may not produce surprising results in your environment. Why Launching Things For Other Users Can Be Bad Back in the old days, you were able to use WMI to launch a...
    • 9 Mar 2011

    Analyzing Networks With PowerShell

    I often need to analyze networks and find machines and IP addresses . So today, I am going to share a set of four (4) very useful PowerShell functions that make it easy to analyze network segments - and they are pretty fast, too! This is the second part of a 2-part-series. Here is the first part btw . Check this out: this simple line just scanned my hotel wlan and returned my neighbors computers IP addresses (and some...
    • 1 Mar 2011

    Accessing Databases from PowerShell

    Recently, I needed to access an SQL database from PowerShell . There are no cmdlets to help you, but with a bunch of helper functions, it is easy and straightforward to access databases (including Microsoft Access files), retrieve data using SQL and even inserting and updating information ( I know I promised to talk about speeding up range pings in your network but this database issue got into my way so I decided to cover...
    • 20 Feb 2011

    Creating IP Ranges (and other type magic)

    Recently I needed to create a range of IP addresses. No big deal, you think? Well, if it is just a small segment, then it *is* easy stuff: 1..255 | Foreach-Object { "192.168.2.$_" } But what if you need larger segments? Type Conversion Magic Ok, you could use nested loops. However, there's also a type that represents IP addresses. It is called System.Net.IPAddress , and it can turn a string into...
    • 17 Feb 2011

    Automatically Surfacing Subproperties

    Sometimes, the true information you are after is burried deep inside object models . For example, when you manage virtual machines with the VMWare cmdlets, Get-VM will get you only very sparse pieces of information. The real goodies are stored inside of subproperties (which was true for the MPEG3 module I created last week as well). Time to get a look at what is going on here (and how you can "move" hidden subproperties...
    • 7 Feb 2011

    Reading and Writing MPEG Tags and Attributes Part 2

    To expand PowerShell functionality and read and write audio/video tags, I have already illustrated how you can access .NET libraries from PowerShell in my previous post. Still, such a .NET library spit out raw .NET objects, and they may not be easy to work with. For example, when you read a media information object from a video file, all the interesting information was still burried deep inside subproperties and nor easily...