There are actually two different ways of manipulating dates and times. Here is the high-level approach to subtract 30 days from today: (Get-Date) -...
database-tools
Creating Time Spans
A time span represents a time interval, and you can use time spans to subtract or add time to a date. For example, to get the date seven days ago:...
Make Function Parameters Mandatory
Mandatory parameters are special because if you do not submit it, PowerShell will automatically ask for it. You can use mandatory parameters in your...
Add Help to Your Functions
You should simply copy and paste the following block comment right above your functions and magically Get-Help works with your functions, too!...
Opening Current Folder in Explorer
Ever wanted to open the current PowerShell folder in an Explorer view? Try this: ii . This will work as long as the current folder is a valid file...
Get Running Process Owners
If you need to filter running processes by owner, for example to terminate the ones owned by some user, you should use WMI and the GetOwner()...
Check for a Battery
If your script needs to know whether your computer has a battery, you can ask WMI. Here is a small function: function Has-Battery {@(Get-WmiObject...
Store Pictures in User Accounts
Have you ever wondered how PowerShell would store a jpeg picture into your Active Directory user account? With the help of some low-level .NET...
Accessing Function Parameters by Type
Adding parameters to your functions is fairly easy. You can add a param() block and specify the parameters. But what if you wanted to assign values...
Creating Your own Types
Did you know that you can compile any .NET source code on the fly and use this to create your own types? Here is an example illustrating how to...
Calculating Server Uptime
Have you ever wanted to find out the effective uptime of your PC (or some servers)? The information can be found inside the event log. Here is an...
Getting Installed Updates
PowerShell is great for parsing log files. Here is a function that extracts all installed Windows updates from an internal log file and returns the...
E-mail-Address-Extractor via RegEx
Regular expressions are extremely powerful - and complex. Fortunately, there are plenty of sources for good regular expressions that describe all...
Accessing WMI Instances in One Line
While you can use Get-WMIObject to query for WMI objects and then select the ones you are really after, you can also cast a WMI object path to a WMI...
Grouping with Script Blocks
Group-Object creates groups based on object properties. For example, you could group processes by company or folder listings by extension: Dir...
Replace Text in Files
Often, some text will need to be replaced in a text file. That's easy with Get-Content and Set-Content - or not? Get-Content c:somefile.txt |...
Sorting Multiple Properties
Sort-Object can sort on multiple properties at the same time. Have a look: Get-Service | Sort-Object Status, Name This will list stopped services...
Discover Hidden Object Members
Get-Member is a great cmdlet to discover object members, but it will not show everything: "Hello" | Get-Member You should add -force to...
Adding Extra Information
Sometimes you may want to tag results returned by a cmdlet with some extra information, such as a reference to some PC name or a timestamp. You can...
Making Objects Read/Write
Whenever you pipe objects through Select-Object, you actually get a copy of the original object. All properties are now readable and writeable, so...
Creating Relative Dates
To create relative dates like "10 days ago," there are two paths. Administrators often add or remove time spans created by New-Timespan:...
Create Remoting Solutions
Whenever you use WMI (Get-WMIObject) to retrieve information, it's a snap to turn a local solution into a remotable solution. You can just add...
How Much RAM Do You Have?
Ever wondered what type of RAM your PC uses and if there is a bank available to add more? Ask WMI! This example also converts the cryptic type codes...
Accessing Object Properties
Objects store information in various properties. There are two approaches if you would like to get to the content of a given property. One is...