PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

Power Tips

Power Tips

Syndication

  • Cleaning Week: Finding Fat Log File Backups

    When you run a Windows box for a couple of months or even years, a lot of disk space can be wasted by backed up log files that are not needed. So if your hard drive runs full, you may want to check how many of these log files you have, and how much space...
  • Cleaning Week: Deleting TEMP Files

    In a previous tip you learned how to check for left-over files in both your own temp folder and the one maintained by Windows. Today, let’s see how these folders can be cleaned up by PowerShell. This code removes all files that haven’t changed...
  • Cleaning Week: Find Data Garbage

    This week is cleaning week, and we’ll show you tactics how you can potentially retrieve gigabytes of disk space, especially with machines that have been running for a while. You may know about your personal temp folder ($env:temp), and maybe you...
  • Finding Paths to Special Locations

    If you’d like to access any of the following special locations, then we have good news for you: PS C:\> [System.Enum]::GetNames([System.Environment+SpecialFolder]) Desktop Programs MyDocuments Personal Favorites Startup Recent SendTo StartMenu...
  • Copy Over PowerShell Remoting Sessions

    You can use PowerShell remoting to transfer files across a network – this is new in PowerShell 5.0. This can be useful in closed environments where you have no SMB file shares. It is not very efficient (and slow) though. Here is an example: # Create...
  • One-Liner Random Password Generator

    Check out this simple way of creating temporary passwords: -join ( ' abcdefghkmnrstuvwxyzABCDEFGHKLMNPRSTUVWXYZ23456789$%&*# ' . ToCharArray () | Get-Random -Count 8 ) You can easily vary the password length: change the number for -Count to...
  • Harvesting Reboot Time from EventLog

    In the previous tip we illustrated how you can ask WMI for the last reboot time of a machine. A more robust way is to query the Windows event logs. Here is how: # get the latest reboot event from the System event log $e = Get-EventLog System -Source Microsoft...
  • Last Bootup Time with Get-CimInstance

    These days, computers hibernate or go into standby but do not really reboot that often anymore. To find out when your computer rebooted the last time, try this line in PowerShell 3.0 or better: Get-CimInstance -Class Win32_OperatingSystem | Select-Object...
  • Extracting Text Information

    Tracert.exe tells you the hops your data packages need to take to get to a given host. Here is an example: PS C:\> tracert www.microsoft.com Tracing route to e10088.dspb.akamaiedge.net [2.16.194.227] over a maximum of 30 hops: 1 8 ms 8 ms 9 ms 1st...
  • Bulk-Convert to String

    Sometimes, commands and methods do not return exactly what you are after. If you, for example, wanted to get the assigned IP addresses for a hostname, you could try this: [ System.Net.DNS ] :: GetHostByName ( ' microsoft.com ' ) . AddressList...
  • Create Local Admin Accounts

    There is no module available for all PowerShell versions that allows to administer local user accounts. Here is a chunk of sample code that illustrates how you nevertheless can automate local account management. The code creates a new local Administrator...
  • Loops and Arrays (and some surprises)

    Frequently, loops are used to retrieve (or generate) data, then save it to a variable. There can be tremendous performance differences though. To generate 10.000 random numbers, for example, you might look into something like this: $numbers = @ () for...
  • Finding Top 3 (of anything)

    Ever wanted to know who are the three worst sources for errors in your System event log? Here is a simple approach that yields the source names with the most errors: Get-EventLog -LogName System -EntryType Error | Group-Object -Property Source | Sort...
  • Converting Fixed-Width Text Data

    PowerShell can turn fixed-width tabular data easily into rich objects. First, define the column widths. Then, have PowerShell inject a delimiter at these positions. Finally, ConvertFrom-Csv can split the text lines and turn the text into rich objects...
  • Exclude Properties

    With Select-Object, you typically include the object properties you want to receive. You can do it the other way around, too, though. Here is an example that includes all properties except some unwanted ones: #requires -Version 2 # get the latest 20 errors...
1 2 3 4 5 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.