Power Tips

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Welcome to the archive of tips delivered through Tobias' Tip of the Day RSS Feed and Your Power Tip of the Day email. Subscribe in the sidebar to get the latest tips!

Sort by: Most Recent | Most Viewed | Most Commented
  • Expanding Variables in Strings

    To insert a variable into a string, you probably know that you can use double quotes like this: $domain = $env:USERDOMAIN $username = $env:USERNAME " $domain\$username " This works well as long as it is clear to PowerShell where your variables...
  • Tag Your Objects with Additional Information

    There may be the need to add additional information to command results. Maybe you get data from different machines and want to keep a reference where the data came from. Or, you want to add a date so you know when the data was created. Tagging objects...
  • Testing for Valid Date

    If you need to test whether some information resembles a valid date format, here is a test function: function Test-Date { param ( [ Parameter ( Mandatory = $true ] $Date ) (( $Date -as [ DateTime ]) -ne $null ) } It uses the -as operator to try and convert...
  • Multiple Assignments in One Line

    When you assign something to a variable, you can enclose the expression in braces. This will also output the data. Have a look: $a = Get-Service ( $a = Get-Service ) See the difference? The second line will not only assign the results from Get-Service...
  • Create New Local Admin Account on the Fly

    Ever needed a new local administrator account for testing purposes? Provided you are already Administrator, and you opened a PowerShell with full Administrator privileges, adding such a user is a matter of just a couple of lines of code: $user = '...
  • Finding Logon Failures

    Whenever someone logs on with invalid credentials, there will be a log entry in the security log. Here is a function that can read these events from the security log (Admin privileges needed). It will then list all the invalid logons found in the log...
  • Finding Logged-On User

    There is a helpful console application called quser.exe which will tell you who is logged on to a machine. The executable returns plain text, but with the help of a little regular expression, this text can be converted to CSV and then imported into PowerShell...
  • Finding Logged-On User on Remote Machine

    In a previous tip we used quser.exe to find the currently logged-on user on the local machine. Here is now a function that also allows us to find the currently logged-on user on a remote machine. As an extra benefit, the returned information is appended...
  • Weird Text Formatting (and what to do about it)

    Check out this code and try to find the problem: $desc = Get-Process -Id $pid | Select-Object -Property Description "PowerShell process description: $desc" This code gets the PowerShell host process and reads the process description, then outputs...
  • Finding Events around A Date

    Often, you might want to browse all system events around a given date. Let's say a machine crashed at 08:47, and you'd like to see all events +/− 2 minutes around that time. Here is a script that does It for you: $deltaminutes = 2 $delta...
  • Using Encrypting File System (EFS) to Protect Passwords

    If you absolutely need to hardcode passwords and other secrets into your scripts (which you should avoid for obvious reasons), then you might still be safe when you encrypt the script with the EFS (Encrypting File System). Encrypted scripts can only be...
  • Logging Script Runtime

    If you'd like to monitor how long a script takes to run, you could use Measure-Command, but this cmdlet is for diagnostic purposes only and omits any script output. Another approach just takes two snapshots and at the end, calculates the time difference...
  • Converting Ticks into Real Date

    Internally, Active Directory uses ticks (100 nanosecond units since 1601) to represent date and time. It has been hard in the past to convert these huge numbers into human readable date and time. Here is a much easier way: [ DateTime ] :: FromFileTime...
  • Getting Group Membership - Fast

    If you need to find out the Active Directory groups your user account is in, typically you would query the Active Directory, and you would have to find the nested group memberships as well. Here is a technique that gets you your own group memberships...
  • Countdown Hours

    All PowerShell Versions Whether it is a birthday or an important game, you may want PowerShell to tell you just how many hours it is till the event starts. Here is how: $result = New-TimeSpan -End ' 2014-12-25 06:45:00 ' $hours = [ Int ] $result...
« First ... < Previous 3 4 5 6 7 Next > ... Last »
Copyright 2012 PowerShell.com. All rights reserved.