Blog - Post List
    • 23 Sep 2016

    Power Tips: Working with UTC Times

    When working across language boundaries, you might want to use a way to “normalize” date and time, for example for logging. Instead of working with local times, it is really simple in PowerShell to convert DateTime objects to universal time: ( Get-Date ) . ToUniversalTime () ReTweet this Tip!
    • 22 Sep 2016

    Power Tips: Displaying Message Boxes

    PowerShell can access all public .NET classes, so it is (fairly) easy to create a message box: $result = [ System.Windows.MessageBox ] :: Show ( ' Do you want to restart? ' , ' Restart ' , ' YesNo ' , ' Warning ' ) $result However, you would need to know the supported values for the parameters. PowerShell can easily wrap this .NET call into a PowerShell function which then provides...
    • 21 Sep 2016

    Power Tips: Using a Stopwatch to Profile Scripts

    Ever wanted to find out how long a particular command or portion of your script took to complete? Here is a simple Stopwatch object that can help you do this: # create a new stopwatch $stopwatch = [ System.Diagnostics.Stopwatch ] :: StartNew () # run a command $info = Get-Hotfix # stop the stopwatch, and report the milliseconds $stopwatch . Stop () $stopwatch . Elapsed . Milliseconds # continue the stopwatch...
    • 20 Sep 2016

    Power Tips: Color Week: Using Token Colors in the PowerShell Console

    PowerShell 3 or better This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. The PowerShell ISE colorizes tokens when you input commands, whereas the PowerShell console does not. If you like colorized tokens, you may want to try the awesome PSReadline module. Here is how you download and install it: PS C:\>...
    • 19 Sep 2016

    Power Tips: Color Week: Changing Error Message Colors in the PowerShell ISE

    This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. In the previous tip we explained how you can change colors for PowerShell’s predefined colors, like error message colors. In the PowerShell ISE, you can specify these colors using the limited 16 console colors: $host . PrivateData . ErrorBackgroundColor...
    • 16 Sep 2016

    Power Tips: Color Week: Changing Error Message Colors

    This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. PowerShell uses predefined colors for errors, warnings, verbose messages, and other output. These colors can be changed, too. This would change error messages to red text with white background. This color scheme is much better to read, especially on presentations...
    • 15 Sep 2016

    Power Tips: Color Week: Using Transparency in the PowerShell ISE Console

    This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. In the previous tips you learned that three settings govern the colors in the PowerShell ISE Console Pane. If you want, you can set a different background color for input and output: $psise . Options . ConsolePaneForegroundColor =[ System.Windows.Media.Colors...
    • 14 Sep 2016

    Power Tips: Color Week: Setting PowerShell ISE Background Color

    This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. In the previous tip we explained how you can set the PowerShell ISE console foreground color, and define the color by specifying red, green, and blue values. The same works for the PowerShell ISE console background color, too, but has some side effects that need...
    • 13 Sep 2016

    Power Tips: Color Week: Using Clear Names for PowerShell ISE Colors

    This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. In the previous tip we changed the PowerShell ISE console foreground color to arbitrary self-defined RGB colors. You can also pick from predefined colors: PS> $psise.Options.ConsolePaneForegroundColor = [System.Windows.Media.Colors]::Azure PS> $psise.Options...
    • 12 Sep 2016

    Power Tips: Color Week: Setting PowerShell ISE Console Colors

    PowerShell 3+ This week we are looking at how you can change coloring both in the PowerShell console and PowerShell ISE so you can fine tune your PowerShell environment. You can change the PowerShell ISE console background and foreground color using the $host object, yet this only gives you 16 predefined colors to choose from: PS> $host.UI.RawUI.ForegroundColor = 'Red' PS> $host.UI.RawUI.ForegroundColor...
    • 9 Sep 2016

    Power Tips: Saving PowerShell User Defaults

    We are about to enter “Color Week” with plenty of tips how you can choose better colors for the PowerShell ISE editor and the console. Most changes that you apply to PowerShell are not saved. PowerShell ISE does save some of the color settings, but a more robust way is to place your customizations in one of PowerShell’s profile scripts. Anything that should be run for any PowerShell host (console as...
    • 8 Sep 2016

    Power Tips: Finding ASCII Codes

    Here is an easy way to find the ASCII code for any character you may have scraped from a website, or found in a script that you copied from the internet. Simply open a PowerShell, and enter this: # paste character(s) inside the quotes $text = '' foreach ( $char in [ char []] $text ) { 'Character {0,-3} Decimal {1,-5} Hex {1,-4:X}' -f $char , [ int ] $char } Next, paste the character...
    • 7 Sep 2016

    Power Tips: Checking Hard Drive Size (Local and Remote)

    WMI can provide the raw data about hard drive size and free space. PowerShell then takes that information and provides user-friendly results like this: PS C:\> # local PS C:\> Get-HardDriveSize DriveLetter Free(GB) Size(GB) Percent ----------- -------- -------- ------- C: 823,7 942,3 87,4 PS C:\> # remote PS C:\> Get-HardDriveSize -ComputerName server2 -Credential server2\Tobias DriveLetter Free(GB...
    • 6 Sep 2016

    Power Tips: Enabling Remote Administration

    PowerShell 2+ Many older DCOM-based commands require a “Remote Administration Firewall Exception” to access remote systems. This includes cmdlets like Get-WmiObject. One easy way of enabling this is to run the following command in a PowerShell with Administrator privileges: netsh firewall set service remoteadmin enable Although this command is considered deprecated, it is still working and one of the...
    • 5 Sep 2016

    Power Tips: Finding Auto Starts

    PowerShell 3+ If you’d like to know which programs start automatically on your machine, WMI may help: PS C:\> Get-CimInstance -ClassName Win32_StartupCommand | Select-Object -Property Name, Location, User, Command, Description Name : OneDrive Location : HKU\S-1-5-21-2012478179-265285931-690539891-1001\SOFTWARE\Microsoft\Windows\CurrentVersion\Run User : DESKTOP-7AAMJLF\tobwe Command : "C:\Users\tobwe...
    • 2 Sep 2016

    Power Tips: Replacing CSV File Headers

    PowerShell 2+ When you read in CSV data and would like to rename the CSV headers, here is a simple approach: just read in the text line by line, and skip the first line (which holds the CSV headers). Then, replace the headers with a list of your own header names: $header = ‘ NewHeader1 ’, ' NewHeader2 ' , ' NewHeader3 ' Get-Content N:\somepathtofile\userlist.csv -Encoding Default | ...
    • 1 Sep 2016

    Power Tips: Receiving Input via Pipeline

    In the previous tip we illustrated how Convert-Umlaut was able to convert special characters in a string. This becomes even more useful if a function accepts pipeline input. Let’s check out the changes required to add that. Without pipeline support, the function looks like this: #requires -Version 3 function Convert-Umlaut { param ( [ Parameter ( Mandatory )] $Text ) $output = $Text . Replace ...
    • 31 Aug 2016

    Power Tips: Replacing Special Chars like “Umlauts”

    PowerShell 2+ Sometimes it becomes necessary to replace special characters like German “Umlauts” in order to normalize user names or email addresses. Here is a little function that illustrates how this can be done: #requires -Version 3 function Convert-Umlaut { param ( [ Parameter ( Mandatory )] $Text ) $output = $Text . Replace ( ' ö ' , ' oe ' ) . Replace ( '...
    • 30 Aug 2016

    Power Tips: Using Friendly Robocopy

    PowerShell 2+ Robocopy is a tool of choice to copy files, and that does not change with PowerShell. You can, however, use PowerShell to embed robocopy inside a user-friendly PowerShell function. This way, you no longer need to remember the awkward command switches used by Robocopy, and instead use PowerShell parameters and IntelliSense. A call to robocopy could then look like this: PS C:\> Invoke-Robocopy ...
    • 29 Aug 2016

    Power Tips: Beware of Changes to PSModulePath

    PowerShell 3+ PowerShell examines the $env:PSModulePath environment variable to find out the locations where it searches for extension modules. A little known fact is that the content of this variable is partially synthesized. And this can become a big issue. When you create a new environment variable called PSModulePath in user level, PowerShell no longer auto-magically adds the default user module path. Some vendors...
    • 26 Aug 2016

    Power Tips: The Truth About WinRM

    PowerShell 3+ The popular winrm command to manage and configure PowerShell Remoting is really just a batch and a VBS file: PS> Get-Command winrm -All | ft -AutoSize CommandType Name Version Source ----------- ---- ------- ------ Application winrm.cmd 0.0.0.0 C:\WINDOWS\system32\winrm.cmd Application winrm.vbs 0.0.0.0 C:\WINDOWS\system32\winrm.vbs Here is a way how you can examine its internals: #requires...
    • 25 Aug 2016

    Power Tips: Listening to Music in the Background

    PowerShell 3+ In the previous tip we presented to you a “Dancing Rick ASCII”, created by Lee Holmes. Lee uses music stored on his server as background. Here is an example that illustrates how you can spawn a background thread, and for example play music received from the Internet. Just run this code, then call Start-Music to start the background music, and Stop-Music to stop it again. function Start...
    • 24 Aug 2016

    Power Tips: Smuggling In PowerShell Code

    PowerShell 2 There is a good reason why Invoke-Expression is considered risky. This cmdlet executes whatever string it gets, and attackers can download malicious code from the Internet, bypass script analysis, and execute it. Here is a benign example of what a simple one-liner can turn into, written by Lee Holmes from the PowerShell team: Invoke-Expression ( New-Object Net.WebClient ) . DownloadString ( ' http...
    • 23 Aug 2016

    Power Tips: Display Windows

    PowerShell 3+ It is fairly easy to use WPF (Windows Presentation Foundation) to create and show simple dialog windows in PowerShell. If you’d like to display a quick message, check this out: Add-Type -AssemblyName PresentationFramework $window = New-Object Windows.Window $window . Title = ' Warning ' $window . WindowStartupLocation = ' CenterScreen ' $window . Topmost = $true $TextBlock...
    • 22 Aug 2016

    Power Tips: Add a Clock to PowerShell

    PowerShell 2+ Here is a fun example that illustrates how to work with timers. It adds a clock to the title bar of the PowerShell console or the PowerShell ISE. Simply run the script, then call Start-Clock to start the clock, and Stop-Clock when you want to get rid of it again. function Start-Clock { # create a timer that fires every 300ms $script:timer = New-Object System.Timers.Timer $timer . Enabled = $true...