Blog - Post List
  • Power Tips: Downloading Information from Internet (Part 2)

    Invoke-WebRequest can download any type of information, and it is up to you to convert it into the format of choice. In the previous tip, we illustrated how to deal with JSON data. Now let’s take a look at webpages that deliver XML data:

    This example retrieves current currency exchange rates from the European Central Bank:

    $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
    $result = Invoke…
    • 16 Apr 2018
  • Power Tips: Downloading Information from Internet (Part 1)

    PowerShell comes with two powerful cmdlets that you can use to retrieve information from the internet. Today, we focus on Invoke-WebRequest.

    This cmdlet serves as a simple web client. Pass it a URL, and it downloads the webpage for you. These simple lines download the psconf.eu agenda for you:

    $page = Invoke-WebRequest -Uri powershell.beer -UseBasicParsing 
    $page.Content
    

    Since it is in JSON format, pipe it to ConvertFrom…

    • 13 Apr 2018
  • Power Tips: Purging Kerberos Tickets for All Accounts

    In the previous tip we covered klist.exe and how it can be used to purge all Kerberos tickets for the current user so that new permissions will take effect immediately.

    While PowerShell can run external apps like klist.exe just fine, things become even more useful when you combine this with other PowerShell commands. This code gets you all logon sessions that do not use NTLM (i.e. Kerberos sessions):

    Get-WmiObject -C…
    • 12 Apr 2018
  • Power Tips: Purging Kerberos Tickets for the Current User

    No need to reboot a system just to apply new permission settings. Instead, purge your Kerberos tickets so that you will get a new ticket based on the current permissions.

    In PowerShell, use this command to purge all cached Kerberos tickets:

     
    PS> klist purge
    
    Current LogonId is 0:0x2af9a
    	Deleting all tickets:
    	Ticket(s) purged!
    
    PS>  
     

    Twitter This Tip! ReTweet this Tip!

    • 11 Apr 2018
  • Power Tips: Creating Hexadecimal Values

    Here are a couple of ways to convert decimal to hexadecimal notation:

    $value = 255
    
    [Convert]::ToString($value, 16)
    '{0:x}' -f $value
    '{0:X}' -f $value
    '{0:x10}' -f $value
    '{0:X10}' -f $value
    

    Twitter This Tip! ReTweet this Tip!

    • 10 Apr 2018
  • Power Tips: Installing PowerShell 6 with Chocolatey

    In the previous tips, we explained how you can download and install Chocolatey, a free Windows package manager that can install software for you.

    Today, we’ll look at how Chocolatey can be used to download and install PowerShell Core 6 so you can test and play with it. This installation will not change your current Windows PowerShell version, so you can safely install it side-by-side.

    To install the PowerShell Core…

    • 9 Apr 2018
  • Power Tips: Using Chocolatey with PowerShell

    Chocolatey is a free package manager for Windows that can be used to download and install software.

    Before you can use Chocolatey from PowerShell, you need to download and install it. If you don’t have administrator privileges, use the script below. It downloads the installation script, checks its digital signature to ensure the file is legit, then runs it:

    # download and save installation script, then check signature…
    • 6 Apr 2018
  • Power Tips: Examining Digital Signature Signers

    When you download a script from the internet, it may contain a digital signature that can help you find out where the script comes from. We looked at this in the previous tip, and this is the code we used: it downloads a PowerShell script to disk, then displays its digital signature:

    # save script to file
    $url = 'https://chocolatey.org/install.ps1'
    $outPath = "$env:temp\installChocolatey.ps1"
    Invoke…
    • 5 Apr 2018
  • Power Tips: Trusting Downloaded Scripts

    Scripts downloaded via Internet have a great potential of being infected with malware, or originate from illegitimate sources. Digital signatures can help add an extra layer of trust and protection.

    As an example, we’ll examine the official “Chocolatey” installation script which is available for download here:

    https://chocolatey.org/install.ps1
    

    When you open this URL in your browser, you’ll see a rather…

    • 4 Apr 2018
  • Power Tips: Execution Policy and Downloaded Script Files

    When you download a file from the internet, it may be tagged by Windows (via NTFS stream), and PowerShell may refuse to execute it:

     
    PS> & "$home\desktop\Rick.ps1"
    & : File C:\Users\tobwe\desktop\Rick.ps1 cannot be loaded. The file C:\Users\tobwe\desktop\Rick.ps1 is not digitally signed. You cannot run this script on the 
    current system. For more information about running scripts and setting execution…
    • 3 Apr 2018
  • Power Tips: The Best Ways to Download Script Files

    Occasionally, PowerShell scripts are made available via direct download. Let’s find the most efficient way to download text-based files via PowerShell. We’ll use the famous “Dancing Rick ASCII” script published by PowerShell Team member Lee Holmes for our examples. It is located here:

    http://bit.ly/e0Mw9w

    When opened in a browser, you’ll see the PowerShell source code as plain text, and the original…

    • 2 Apr 2018
  • Power Tips: Colorizing Cmdlet Output

    Starting in PowerShell 5.1, the PowerShell console supports VT escape sequences that can be used to position and format console text. Note that this works in the console only, not the PowerShell ISE. Note also that you either need Windows 10 or an emulator like ConEmu.

    When you submit a hash table to Select-Object, the hash table can produce “calculated” columns. It provides two pieces of information: Name (the column…

    • 30 Mar 2018
  • Power Tips: Using Colors in PowerShell Console

    Starting in PowerShell 5.1, the PowerShell console supports VT escape sequences that can be used to position and format console text. Note that this works in the console only, not the PowerShell ISE. Note also that you either need Windows 10 or an emulator like ConEmu.

    To colorize text, you can always use Write-Host and its -ForegroundColor and -BackgroundColor properties in any PowerShell version:

    foreach($color1 in
    • 29 Mar 2018
  • Power Tips: Positioning the PowerShell Console Cursor

    Starting in PowerShell 5.1, the PowerShell console supports VT escape sequences that can be used to position and format console text. Note that this works in the console only, not the PowerShell ISE. Note also that you either need Windows 10 or an emulator like ConEmu.

    VT escape sequences can set the console cursor to any location inside the console window. To set the caret to the top left corner, for example, use this…

    • 28 Mar 2018
  • Power Tips: Output Log Messages in the Same Line

    Starting in PowerShell 5.1, the PowerShell console supports VT escape sequences that can be used to position and format console text. Note that this works in the console only, not the PowerShell ISE. Note also that you either need Windows 10 or an emulator like ConEmu.

    VT escape sequences can set the console cursor to any position inside the current line. This way, you can easily create a function that outputs status…

    • 27 Mar 2018
  • Power Tips: Using Underlined Console Output

    Starting in PowerShell 5.1, the PowerShell console supports VT escape sequences that can be used to position and format console text. Note that this works in the console only, not the PowerShell ISE. Note also that you either need Windows 10 or an emulator like ConEmu.

    To try this, run the code below in a PowerShell console:

    $esc = [char]27
    "$esc[4mOutput is now underlined!"
    

    PowerShell now underlines all output…

    • 26 Mar 2018
  • Power Tips: Safely Embedding Variables

    When you use double quotes in PowerShell, you can add variables to a string, and PowerShell automatically replaces these with their content – that’s no news:

    $ID = 234
    
    "Server $ID Rack12"
    

    However, PowerShell automatically decides when a variable ends, so if you wanted to add a number in between a text without spaces, this would fail:

    $ID = 234
    
    "Server$IDRack12"
    

    As the color coding suggests…

    • 23 Mar 2018
  • Power Tips: Synthesizing Speech – Using Different Voices (Part 4)

    Windows 10 comes with excellent text-to-speech support and different high-quality voices. To find out which voices are available, try this:

    Add-Type -AssemblyName System.speech
    $synthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer
    
    $synthesizer.GetInstalledVoices().VoiceInfo | 
      Where-Object { $_.Name -notlike 'Microsoft Server*' } |
      Select-Object -Property Name, Gender, Age, Culture
    

    The result…

    • 22 Mar 2018
  • Power Tips: Synthesizing Speech – Using Speech Synthesis Markup Language SSML (Part 3)

    Windows built-in text-to-speech engine accepts plain text and turns it into a voice, but it can also be controlled using “Speech Synthesis Markup Language”. This way, you can fine-tune the voice, control pitch, and also language.

    Windows ships with localized speech engines, so controlling the language is a good idea. Else, your (English) text may sound weird on a (German) system.

    Add-Type -AssemblyName System…
    • 21 Mar 2018
  • Power Tips: Synthesizing Speech – Recording to File (Part 2)

    In the previous tip we introduced the text-to-speech engine. This engine can save your text to a WAV sound file so you can use it to generate spoken messages:

    $Path = "$home\Desktop\clickme.wav"
    
    Add-Type -AssemblyName System.Speech
    $synthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $synthesizer.SetOutputToWaveFile($Path)
    $synthesizer.Rate = -10
    $synthesizer.Speak('Uh, I am not feeling that…
    • 20 Mar 2018
  • Power Tips: Synthesizing Speech (Part 1)

    In the previous tips, we explained how PowerShell can generate acoustic signals by playing system sounds or WAV sound files. PowerShell can also use the built-in speech synthesizer:

    Add-Type -AssemblyName System.speech
    $synthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $synthesizer.Speak('Hello! I am your computer!')
    

    Note that Windows 10 ships with localized text-to-speech engines, so if your…

    • 19 Mar 2018
  • Power Tips: Playing Sound Files

    In the previous tip we explained how PowerShell can play system sounds. For a bit more flexibility, PowerShell can also play arbitrary *.wav sound files:

    $soundPlayer = New-Object System.Media.SoundPlayer
    $soundPlayer.SoundLocation="$env:windir\Media\notify.wav"
    $soundPlayer.Play()
    

    By default, PowerShell does not wait for the sound to be played. If you’d like to play it synchronously, try this:

    $soun…
    • 16 Mar 2018
  • Power Tips: Playing Sounds

    If all you need is a beep, then PowerShell can help you easily:

    $frequency = 800
    $durationMS = 2000
    [console]::Beep($frequency, $durationMS)
    

    If you’d like something more sophisticated, then you can also have PowerShell play a system sound:

    [System.Media.SystemSounds]::Asterisk.Play()
    

    Here is a list of supported system sounds:

     
    PS> [System.Media.SystemSounds] | Get-Member -Static -MemberType Property
    
    
       TypeName…
    • 15 Mar 2018
  • Power Tips: Sort IPv4 and IPv6 Addresses Correctly

    When you try and sort IPv4 addresses via Sort-Object, this fails:

     
    PS> '10.1.2.3', '2.3.4.5', '1.2.3.4' | Sort-Object
    1.2.3.4
    10.1.2.3
    2.3.4.5 
     

    This is no surprise because the data is of type “string”, so Sort-Object uses alphanumeric sorting. In the previous tip we showed how you can cast the data to [Version] and pretend you are sorting software versions:

     
    PS> '10.1.2.3', …
    • 14 Mar 2018
  • Power Tips: Sort IPv4 Addresses Correctly

    In the previous tip we published a super-fast function called Test-OnlineFast, and this function was able to ping an entire IP segment in record time:

     
    PS> $iprange = 1..200 | ForEach-Object { "192.168.189.$_" }
    
    PS> Test-OnlineFast -ComputerName $iprange
    
    Address         Online DNSName                            Status           
    -------         ------ -------                            ------           
    192.168.189…
    • 13 Mar 2018