PowerShell hash tables are, by default, not case sensitive: PS > $hash = @{} PS > $hash.Key = 1 PS > $hash.keY = 2 PS > $hash.KEY 2 If...
Powershell
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 – Faster!
In a previous tip, we showed you how to parse text-based log files using the PowerShell pipeline. However, the pipeline has considerable overhead....
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...
Restart required
Check out this line to determine when an installation required a system restart: Get-EventLog -InstanceId 1038 -LogName Application | ForEach-Object...
Finding multiple RegEx matches
In a previous tip, you learned that Select-Object can find multiple matches. Here is a function called matches. You can submit a regular expressions...
Find multiple matches
When you want to find matches based on regular expressions, PowerShell will only support the -match operator which finds the first match. There does...
Translate EventID to InstanceID
Sometimes, you may need to have the event ID for a system event, though what you really need is the instance ID. For example, Get-EventLog will only...
Windows license validation
Search for the appropriate events in your event log to discover when Windows has validated your license: get-eventlog -LogName Application...
Analyze automatic defragmentation
Check out this line to visualize when your system defragmented your hard drives: get-eventlog -LogName Application -InstanceId 258 | ForEach-Object...
Find system restore points
Windows Update and Software installations will frequently create system restore points. Run this to get a list of such events: Get-EventLog -LogName...
Secret history shortcut
PowerShell will keep a history of the commands you entered and then you can list the history with Get-History, configuring the maximum length of...
Error handling for native commands
When you need to handle errors created by native commands, you can use a wrapper function like Call. It will automatically discover when a native...