Blog - Post List
    • 21 Sep 2017

    Power Tips: Uncovering TinyUrls’ True Origin

    URL shortening is great for Twitter messages but hides the origin. Would you really trust http://bit.ly/e0Mw9w? Here is a simple approach that lets you uncover where a tiny URL really points to: $shortUrl = "http://bit.ly/e0Mw9w" $longURL = Invoke-WebRequest -Uri "http://untiny.me/api/1.0/extract?url=$shortUrl&format=text" -UseBasicParsing | Select-Object -ExpandProperty Content "'...
    • 20 Sep 2017

    Power Tips: Creating Colorful Weather Report

    In the previous tip we explained how you can retrieve weather forecast data using Invoke-WebRequest. This was pure black-and-white text. To get a colorful report, on Windows 10 you can take advantage of control sequences supported by powershell.exe. Simply run the code below inside a PowerShell console: $City = ' Hannover ' ( Invoke-WebRequest "http://wttr.in/$City" -UserAgent curl ) . content...
    • 19 Sep 2017

    Power Tips: Get Weather Forecast

    Invoke-WebRequest can easily retrieve web page content for you. If you run it without the –UseBasicParsing parameter, the HTML is even parsed by the Internet Explorer DOM. This way, PowerShell requires just a couple lines of code to retrieve the current weather forecast for almost any city in the world: $City = ' New York ' $weather = Invoke-WebRequest -Uri "http://wttr.in/$City" $text =...
    • 18 Sep 2017

    Power Tips: Avoid Read-Host

    Do you use Read-Host to receive user input? If you do, rethink. Read-Host always prompts a user, and there is no way to automate scripts that use Read-Host: $City = Read-Host -Prompt ' Enter City ' A much better easy way would look like this: param ( [ Parameter ( Mandatory )] $City ) This creates a mandatory parameter. If the user does not supply it, a prompt is created, very similar to Read-Host...
    • 15 Sep 2017

    Power Tips: Using Online Help

    PowerShell does not ship with help files, and installing help files locally requires Administrator privileges. A much easier approach can often be to visit the online help for a cmdlet like this: Get-Help -Name Get-Acl -Online This opens a browser and navigates to the online help. Online help often is more up-to-date and easily approachable. It does require internet access, obviously, and there is not always online...
    • 14 Sep 2017

    Power Tips: Outputting and Assigning at the same time

    In the previous tip we talked about logging script results, and how you can assign values and at the same time output the assigned values by using parentheses: PS> ($a = Get-Process -Id $pid) Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1595 102 283200 325444 64,56 6436 1 powershell_ise PS> $a Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName...
    • 13 Sep 2017

    Power Tips: Logging Script Output

    There are numerous ways to log script output but one especially lazy technique is to run Start-Transcript. In PowerShell 5, this cmdlet is supported in all full hosts, not just powershell.exe, so you can use it in PowerShell ISE or other editors as well. In addition, transcripts can be nested, so when you write a script, you can safely add Start-Transcript at the beginning and Stop-Transcript at the end. Start-Transcript...
    • 12 Sep 2017

    Power Tips: Installing Modules in PowerShell Core on Linux

    When you’d like to install modules for all users from the PowerShellGet repository, you need Administrator privileges. On PowerShell Core on Linux, you can use the command “sudo” to enable Administrator privileges, and run PowerShell. Just make sure you specify the command in braces. This would install the module AzureRM.NetCore from the PowerShell Gallery with administrator privileges for all users...
    • 11 Sep 2017

    Power Tips: “Braces Secret” with PowerShell.exe

    Here is a little secret that applies to powershell.exe when it is called from within PowerShell or PowerShell Core: when you run powershell.exe and submit commands via the parameter -Command, PowerShell runs the command and returns plain text: $a = powershell -noprofile -Command Get-Service $a [ 0 ] . GetType () . FullName System.String When you place your code in braces, PowerShell will serialize the results...
    • 8 Sep 2017

    Power Tips: Parsing Distinguished Names

    Distinguished names are strings, and strings contain powerful ways of parsing data. The most powerful yet simple approach is the Split() method. Check out how easy it is to get back the name of the last element of a distinguished name using Split(): $dn = ' CN=pshero010,CN=Users,DC=powershell,DC=local ' $lastElement = $dn . Split ( ' , ' )[ 0 ] . Split ( ' = ' )[ - 1 ] $lastElement Split...
    • 7 Sep 2017

    Power Tips: Auditing Logons

    Have you ever wondered whether someone has logged into your PC while you were away? In a previous tip we explained how you can examine the rich auditing information found in the Windows Security log, provided you have Administrator privileges. To find out who logged into your PC, try the code below! The function Get-LogonInfo searches for security events with ID 4624. Security information is protected, so you need to...
    • 6 Sep 2017

    Power Tips: Finding UAC Elevations

    The Windows “Security” log contains rich audit information. By default, it logs all requests for privilege elevation which occurs when you run a program with full Administrator privileges. To get a list of elevations that occurred on your machine, try this: #requires -RunAsAdministrator function Get-ElevationInfo { param ( [ DateTime ] $Before , [ DateTime ] $After , [ string []] $ComputerName...
    • 5 Sep 2017

    Power Tips: How to Correctly Wrap Multiple Results

    Whenever a PowerShell function needs to return more than one kind of information, it is important to wrap them as objects. Only then will the caller be able to discover and uniquely access the information. Here is a quick sample. This is a function that simply outputs 3 pieces of data. They are returned as an array of different objects: function test { 33.9 "Hallo" Get-Date } $result = test ...
    • 4 Sep 2017

    Power Tips: Explore WMI

    Get-WmiObject and Get-CimInstance both can provide you with a lot of valuable information, provided you know the name of WMI classes to query. Here is a quick PowerShell function called Explore-WMI which helps you find useful WMI class names: function Explore-WMI { # find all WMI classes that start with "Win32_"... $class = Get-WmiObject -Class Win32_ * -List | # exclude performance counter classes...
    • 1 Sep 2017

    Power Tips: Registering Default PowerShell Repository

    If you use the PowerShellGet module (which ships by default on Windows 10 and Server 2016), you can easily download and install shared PowerShell scripts and modules: PS> Find-Module -Tag Security Version Name Repository Description ------- ---- ---------- ----------- 2.5.0 Carbon PSGallery Carbon is a PowerShell module for automating t... 0.8.1 ACMESharp PSGallery Client library for the ACME protocol, which...
    • 31 Aug 2017

    Power Tips: Find Installed Software

    Most installed software registers itself in one of four places inside the Windows Registry. Here is a quick PowerShell function called Get-InstalledSoftware that queries all of these keys, and outputs information about found software. function Get-InstalledSoftware { param ( $DisplayName = ' * ' , $DisplayVersion = ' * ' , $UninstallString = ' * ' , $InstallDate = ' * '...
    • 30 Aug 2017

    Power Tips: Disable OneDrive in Windows 10

    Are you also irritated by the OneDrive icon found in the navigation tree in explorer? If you never use OneDrive, here are two easy-to-use PowerShell functions that let you hide (and show again) the OneDrive icons in explorer: function Disable-OneDrive { $regkey1 = ' Registry::HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6} ' $regkey2 = ' Registry::HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66...
    • 29 Aug 2017

    Power Tips: Alternate Get-Service

    The cmdlet Get-Service has a number of drawbacks. For example, there is no parameter to filter running or stopped services, and the results do not include the service startmode. WMI can deliver such information. Here is a simple function that gets the most often needed service information: function Get-ServiceWithWMI { param ( $Name = ' * ' , $State = ' * ' , $StartMode = ' * ' ...
    • 28 Aug 2017

    Power Tips: Robocopy Light

    Robocopy.exe is an extremely powerful and versatile built-in command to copy files efficiently from one location to another. Unfortunately, this command has so many options and switches that very often make it hard to use. If all you want is to copy files from A to B, here is a simple PowerShell function that wraps robocopy and turns the beast into a simple-to-use copy command: function Copy-FileWithRobocopy { param...
    • 25 Aug 2017

    Power Tips: Create Summary Objects for Inventory

    Beginning in PowerShell 3, a PSCustomObject can easily combine valuable information that you gathered from different sources. Here is an example that gets various information from different WMI classes, and produces one inventory object that can then be passed on or worked with: # get information from this computer $Computername = "." # get basic information (i.e. from WMI) $comp = Get-WmiObject -Class...
    • 24 Aug 2017

    Power Tips: Scraping Quotes from WikiQuote

    Here is a fun script that may not work forever. It takes one or more topics you choose, then returns one or more random quotes taken from the wikiquote webpages: PS> Get-Quote Text ---- If you don't know anything about computers, just remember that they are machines that do exactly w... PS> Get-Quote -Topics men Text Author ---- ------ But man is not made for defeat. A man can be destroyed but not...
    • 23 Aug 2017

    Power Tips: Controlling Audio (Mute and Volume)

    Ole Morten Didriksen has dug out some API calls that enable audio volume control ( https://gist.github.com/oledid ). With this, it is trivial from PowerShell to mute, unmute, or set the audio volume: #requires -Version 2.0 Add-Type -TypeDefinition @' using System.Runtime.InteropServices; [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IAudioEndpointVolume...
    • 22 Aug 2017

    Power Tips: A better Get-History

    When you type “h” in PowerShell, you see the history of commands you entered during your session. Inspired by Pratek Singh (https://geekeefy.wordpress.com/2017/06/20/powershell-get-history/), here is a clever alternative called “h+” that opens up a grid view window instead and lets you pick the commands from your history. Hold CTRL to select multiple items. Pratek invoked all selected items using...
    • 21 Aug 2017

    Power Tips: Getting MAC Vendor List

    Prateek Singh has invested some effort in creating a sanitized MAC vendor address list in CSV format which can be found in his blog ( https://geekeefy.wordpress.com/2017/07/06/get-mac-vendor-using-powershell/ ). Such lists can be very useful to identify the vendor of network devices by looking at their MAC addresses. Using PowerShell, you can easily download it to your computer: #requires -Version 3.0 $url = '...
    • 18 Aug 2017

    Power Tips: Creating Excel Reports (Part 3 – Individually Accessing Workbook)

    Sometimes you may want to create irregularly shaped reports, that is reports in a non-table design. For this, PowerShell lets you connect to the Excel object model. This way, you can access individual cells, read and write their content, and even format them. This gives you maximum flexibility. However, the drawback is that it requires a lot of code since you have to manually set up every single workbook cell. Also...