• Downloading Information from Internet (Part 8)

    In the previous tip we illustrated how to download files from the internet using Invoke-WebRequest. However, this works for HTTP addresses only. Once you start using HTTPS addresses, it fails:

    $url = "https://github.com/PowerShellConferenceEU/2018/raw/master/Agenda_psconfeu_2018.pdf"
    $destination = "$home\agenda.pdf"
    
    Invoke-WebRequest -Uri $url -OutFile $destination -UseBasicParsing
    Invoke-Item -…
    • 24 Apr 2018
  • Downloading Information from Internet (Part 7)

    In this part of this mini-series, we are showing you how Invoke-WebRequest can download files from the internet for you. Simply use the parameter -OutFile. This code downloads a PowerShell icon as PNG image onto your desktop:

    $url = "http://www.dotnet-lexikon.de/grafik/Lexikon/Windowspowershell.png"
    $destination = "$home\powershell.png"
    
    Invoke-WebRequest -Uri $url -OutFile $destination -UseBasicP…
    • 23 Apr 2018
  • Downloading Information from Internet (Part 6)

    In the previous tip we explained how you can retrieve XML data from webpages using either Invoke-WebRequest or Invoke-RestMethod. For XML data, there is also another approach which uses the built-in methods found in the XML object itself.

    This was the approach with Invoke-RestMethod:

    $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
    (Invoke-RestMethod -Uri $url -UseBasicParsing).Envelope.C…
    • 20 Apr 2018
  • Downloading Information from Internet (Part 5)

    In the previous tip we illustrated how Invoke-WebRequest can be used to download JSON or XML data from a web page. This example downloads the psconf.eu agenda in JSON format:

    $page = Invoke-WebRequest -Uri powershell.beer -UseBasicParsing 
    $($page.Content | ConvertFrom-Json) | Out-GridView
    

    And this example downloads currency exchange rates in XML format:

    $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref…
    • 19 Apr 2018
  • Downloading Information from Internet (Part 4)

    In the previous tip we explained how you can use Invoke-WebRequest to download data from webpages, and for example retrieve excuses from a webpage that serves random excuses. However, when you try this, you might get back always the same excuse (or data).

    $url = 'http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl'
    $page = Invoke-WebRequest -Uri $url -UseBasicParsing
    $content = $page.Content
    
    $pattern = '…
    • 18 Apr 2018
  • Downloading Information from Internet (Part 3)

    In previous tips, we showed how to use Invoke-WebRequest to download data from webpages, and process data delivered in JSON or XML format. Most webpages contain plain HTML data, however. You can use regular expressions to pick information from plain HTML.

    This is how you get to webpage content:

    $url = 'http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl'
    $page = Invoke-WebRequest -Uri $url -UseBasicParsing
    $…
    • 17 Apr 2018
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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