database-tools

List NTFS Permissions

To view NTFS permissions for folders or files, use Get-Acl. It won't show you the actual permissions at first, but you can make them visible...

Duplicate Output

Sometimes, you may want to store command results in a variable and at the same time, output it to the console. Once you assign results to a...

Multiple Text Replace (Fast)

In a previous tip, we showed you how to replace multiple different characters in a text using the Switch statement. While this works well, it is not...

Speed Up Loops

First, compare these two code samples: $array = 1..10000 Measure-Command { for ($x=0

Set Clipboard

If you are using Windows Vista or better, you can pipe text to clip.exe  to copy it to your clipboard: Dir $env:windir | clip   Here is...

Get-Clipboard

If your PowerShell host uses the STA mode, you can easily read clipboard content like this: function Get-Clipboard { if...

Test for STA mode

By default, the PowerShell console does not use the STA mode whereas the ISE editor does. STA is needed to run Windows Presentation Foundation...

Multiple Text Replace

Imagine that you need to replace a number of different characters in a text. For example, you need to remove special characters or escape something....

Switch Accepts Arrays

Did you know that the Switch statement can accept arrays? Use this sample to translate numbers into words: PS> switch ( 1,5,2,4,3,1 ) { 1 {...

Check For Numeric Characters

Try this if you need to check a single character and find out whether or not it is numeric: PS > [char]::IsNumber('1') True PS >...

Check Array Content With Wildcards

You may know the -contains operator. Try using it to check whether an array contains a specific element: PS > $names = dir $env:windir |...

Re-Assigning Types to Variables

You can no longer assign other types when you strongly type a variable: [Int]$a = 1 $a = 'does not work'   However, you can always...

Generate Random Passwords

In a previous tip, we showed you how to create random passwords. Thanks to your feedback, here is an even shorter version: -join...

Tile Windows

Do you remember the ancient feature where you could tile or cascade all open windows? These features are still available. However, before you try...

Case-Sensitive Hash Tables

PowerShell hash tables are, by default, not case sensitive: PS > $hash = @{} PS > $hash.Key = 1 PS > $hash.keY = 2 PS > $hash.KEY 2 If...

Creating Byte Arrays

You can try this to create a new empty byte array with 100 bytes: $byte = New-Object Byte[] 100   Try this if you need to create a byte array...

Quickly Changing File Extensions

If you want to quickly exchange a file extension to create a "bak" backup version or generate a new file name for output, you should use...

Grouping Files Based On Size

In a previous tip, we showed you how to group using your own criteria. Group-Object can also auto-create hash tables so that you can easily create...

Grouping Using Custom Criteria

Try using Group-Object to group objects by any property: PS> Get-Process | Group-Object -property Company You can also submit a script block and...

Parsing Text-Based Log Files

Extracting useful information from huge text-based log files isn’t necessarily  a difficult task. Check out this line: PS> Get-Content...

Finding IP Addresses

Sometimes, classic tools like ipconfig.exe can yield useful information that you'd like to integrate with PowerShell. Here is sample code that...

Calculate Total Folder Size

Here is a useful function that you can use to calculate the total size of a folder: function Get-FolderSize($path) { Get-ChildItem $path -Recurse...

Use WMI to Create Hardware Inventory

You can use Get-WMIObject to create a high-level hardware report. Instead of submitting a specific device class name, you should use a generic...

Remove Keys From a Hash Table

Once you create a hash table, it is easy for you to add new key-value pairs like this: PS > $myHash = @{} PS > $myHash.Name = 'Tobias'...

Copy Registry Hives

You can use Copy-Item to quickly copy entire structures of registry keys and sub-keys in a matter of milliseconds. Take a look at this example...

Getting Full Admin Privileges

You may sometimes get "Access Denied" exceptions, even though  you have Admin privileges and used an elevated shell. For ...

RegEx Magic

The [RegEx] type has a method called Replace(), which can be used to replace text by using regular expressions. This line would replace the last...

Find Dependent Services

If you would like to check the implications of stopping  a service, you should have a look at its dependent services: (Get-Service...