Manipulating File System Paths (Part 2)

When you turn a path into an array to manipulate parts of the path, if you access path elements by fixed index numbers, then this approach will only work if the path has a fixed number of subfolders. To work with any path length, try using variables. This example will...

Manipulating File System Paths

PowerShell lets you access multiple array elements. With the help of -split and -join, you can easily manipulate file system paths this way. To exclude the second and third level folder, try this: $path = 'C:\users\Tobias\Desktop\functions.ps1' $array = $path...

Writing DWORD-Values to Registry

In PowerShell 2.0, Set-ItemProperty by default always creates REG_SZ (String) values. PowerShell 3.0 is smarter. When you submit a numeric value, then PowerShell 3.0 automatically creates a DWORD value. If you submit a non-numeric value, a REG_SZ is created. Try for...

Importing Website Tables into Excel

Sometimes, you might see interesting information on websites. For example, navigating to http://www.ssa.gov/OACT/babynames/index.html will open a site with popular baby names. If you'd like to work with that data, the traditional approach is to read the raw HTML...

Negating Variables

Let's assume you have a variable with a negative value, and you'd like to make it a positive value. Here's a simple way: Basically, this approach negates the variable, so you can do the opposite as well: Note that this will not change $a unless you...

Determining Registry Value Data Type

PowerShell can read and write Registry keys and values easily, but there is no a built-in way to check the data type of a given value. To find out the data type of a value, first access the Registry key as usual. Then, use its GetValueKind() method. This example...

Creating Colorized Excel Output (Part 2)

In a previous tip, we illustrated how you can feed HTML data to Excel in order to create formatted Excel sheets. Today, we'll show you an easy way to create color-coded data. Instead of having PowerShell create HTML (via ConvertTo-Html), you can create the HTML...

Creating Readable CSV-and HTML-Output

When you convert PowerShell results to CSV or HTML output, you may have discovered that some properties don't display correctly. PowerShell cannot correctly convert arrays to strings and instead displays the array data type. Here's a sample: # use extension...

Creating Colorized Excel Output

PowerShell can send data to Excel using CSV files easily. Here's a short script creating a list of running services and opening it in Excel (provided you have Excel installed of course): $Path = "$env:temp\tempfile.csv" Get-Service | Export-Csv -Path...

Finding Unused Drive Letter

Here's a simple way of finding unassigned drive letters: If you just want any unused drive letter, pipe the result to Get-Random. This approach uses the fact that PowerShell creates a function for each available drive, so the code tests whether or not there is a...

Using Bitwise Shift Operators

PowerShell 3.0 introduces two new operators to bitwise shift. You can use these, for example, to convert GB to MB or KB to GB: -shr 10 basically does the same as dividing by 1024, whereas -shr 20 divides by 1MB. Likewise, -shl does the opposite. ReTweet this Tip!

Preventing Debugging (Part 2)

In a previous tip we explained how you can tell PowerShell not to debug certain functions by adding a special attribute: function test { [System.Diagnostics.DebuggerHidden()] param($a) Get-Service } So when you step through code using the PowerShell debugger, the...

Adding Suggestions to PowerShell Console

Suggestions are little pieces of text that can appear next to a PowerShell error message to better explain what went wrong. Suggestions only work in the PowerShell console. They do not work in the ISE editor. There are only three suggestions hard-coded into...

Printing from an Android device using FireMonkey

Printing from an Android device using FireMonkey

Printing from an Android device requires a little bit more setup than in iOS where you simply connect to an AirPrint capable printer (like my Epson XP-400). However, Android printing appears to work with any Wifi printer (or PC connected printer), so its more...

Examining Certificates

Here's how you can pick a certificate from your certificate store. This line will return certificates that have "Tobias" in the subject: Next, you can "unfold" all properties and display them as plain text: Or, you can display only selected...

Removing Certificate Plus Private Key

Starting with PowerShell 3.0, the certificate provider has become a lot more powerful. It can now easily delete certificates plus their associated private key (which you should only try if you clearly understand what it means to get rid of a digital certificate). This...

Preventing Flashing Console Window

When you run a console command inside a non-console PowerShell host (like the ISE editor), there will be a flashing window, indicating that the host is launching a temporary console window to host the console command. That's no bad thing, but if you must prevent...

Changing Current Time Zone

In a previous tip you learned how PowerShell can list all available time zone IDs. Today, we show you how to set the time zone using the utility tzutil.exe. First, here's a helper function that returns the correct time zone ID when you submit part of its name:...

Get Time Zone Info

There is a little known utility called tzutil.exe which can both set the current time zone and get all available time zones. Today, let's check out how PowerShell can embrace raw output and help you get what you want. When you dump all available time zones, you...

Finding Office Installation Path

Microsoft Office has many different versions and exists as a 32-bit and 64-bit edition. Finding the installation path with PowerShell can still be surprisingly simple once you understand the power of wildcards. The next line will return the Excel installation path...