When disk space gets low, you may want to clean up your temporary folder. The code deletes all files that are older than 30 days to make sure...
Powershell
Filtering Files or Folders
To filter folder content by file or folder, check whether the Length property is present. It is present for files and missing in folders: Dir...
Office365 Cmdlets
Office365 comes with a complete set of PowerShell cmdlets. To review the cmdlets and what you can do with them, visit this URL:...
Forwarding Parameters
To forward function parameters to a cmdlet, use $psboundparameters automatic variable and splatting. Here is a sample: function...
How Long Has Shell Been Running?
To find out how long your PowerShell session has been running, try this code snippet: ((((Get-Date)-(Get-Process -id $pid).starttime) -as [string])...
Enabling Remote WMI and DCOM
Many cmdlets have a built-in -ComputerName parameter that will allow for remote access without using the new PowerShell remoting. For this to work,...
Creating Local Admins (Part 2)
In a previous tip, we showed you how to create a new local admin account using ADSI. On Windows 7 and Server 2008, it is much easier to use net.exe...
Creating Local Admins
Here is a piece of code that will create a local user account and put it into the local Administrators group: $computername = $env:computername #...
Adding Members to Local Group
To manage local groups, you can think about using net.exe. It may be much easier than using COM interfaces. The next line will add a local user...
Bulk Renaming Files
Rename-Item can rename hundreds of files in one step. Have a look: $global:i = 1 dir c:\test1\ -Filter cover*.jpg | Rename-Item -NewName {...
Monitoring Folder Content
You can use a FileSystemWatcher object to monitor a folder and write a log for all newly created files. For example, this can be used as an...
Renaming Computers
PowerShell can also rename computers. The next example will read the serial number from the system enclosure class and rename the computer...
Returning Array in One Chunk
In a previous tip, we showed you how adding a single comma can change the way functions return arrays. With this trick, you can force a PowerShell...
Return Arrays
Normally, PowerShell will not preserve the type of an array returned by a function. It is always reduced to Object[]: function test {...
Put a PowerShell Shortcut on Your Desktop
Here is how you can create new shortcuts, such as a desktop shortcut to PowerShell: $shell = New-Object -ComObject WScript.Shell $lnk =...
Validating Function Parameters
You can use Regular Expression patterns to validate function parameters: function Get-ZIPCode { param( [ValidatePattern('^\d{5}$')] [String]...
Re-Encoding ISE-Scripts in UTF8
PowerShell ISE by default saves .PS1 scripts in a highly unusual big endian Unicode encoding. For example, you cannot digitally sign such files.....
Solving Problems with Parenthesis
You use parenthesis in PowerShell to control an execution order. However, some language keywords are not legal inside parenthesis, like try and...
Removing CSV Headers
Try this to remove column headers from a CSV file: $result = get-process | ConvertTo-Csv -Delimiter ";" $count = $result.Count -1...
Check Whether a Program is Running
If you'd like to find out whether an instance of WinWord (or any other program) is currently running, you can try this: (Get-Process winword -ea...
Finding Files
Dir (Get-Childitem) is a simple, but effective way to find files. The following line will find any file or folder called "hosts" anywhere...
Opening Results in Excel
To send PowerShell results to Excel, you can use CSV-files and automate the entire process by using this function: function...
Creating Self-Updatable Variables
If you want a variable to update its content every time you retrieve it, you can assign a breakpoint and an action to it. The action script block...
Open File Exclusively
To open a file in a locked state so no one else can open, access, read, or write the file, you can use the low-level .NET methods like this: $path =...