• Managing Installed Modules (Part 2)

    Whenever you install new modules via Install-Module or update existing modules via Update-Module, new module versions are installed side-by-side.

    If you always work with the latest versions of modules and do not need to access a specific older version, you might want to find outdated modules and remove them:

    # get all installed modules as a hash table
    # each key holds all versions of a given module
    $list = Get-Instal…
    • 20 Nov 2020
  • Managing Installed Modules (Part 1)

    When you install new PowerShell modules via Install-Module, PowerShell remembers the install location. So it is easy to get a list of modules you installed via Install-Module:

     
    PS> Get-InstalledModule
    
    Version Name                       Repository Description                                               
    ------- ----                       ---------- -----------                                               
    2.7.1.9…
    • 18 Nov 2020
  • Repairing PowerShell Gallery Access

    The PowerShell Gallery (www.powershellgallery.com) is a perfect place to look for new PowerShell commands. With Install-Module, you can easily download and install new PowerShell modules.

    However, sometimes things fail, and there are two primary reasons why.

    Occasionally, the original PowerShellGet module shipping with Windows 10 is outdated. You then get exceptions complaining about missing or wrong parameters.

    To solve…

    • 16 Nov 2020
  • Creating Icons

    In the previous tip we showed how you can fine-tune „Windows Terminal“ and add new entries to the list of launchable applications. If you want to add icons for these entries, you need appropriate icon files.

    Here is some PowerShell code that extracts icons from executables. You can use the generated ICO-files in Windows Terminal and elsewhere.

    # create output folder
    $destination = "c:\icons"
    mkd…
    • 12 Nov 2020
  • Fine-Tuning Windows Terminal

    In the previous tips we explained how you can install „Windows Terminal“ on Windows 10 via the Microsoft Store. Windows Terminal features PowerShell consoles in separate tabs and is very useful.

    You can control the types of consoles available in the tab drop-down list by editing the settings file: in Windows Terminal, in the title bar click the down arrow button, and choose „Settings“. This opens a JSON file…

    • 10 Nov 2020
  • Turn Windows Terminal into a Portable App

    On Windows 10, there is a new and awesome tool available for any PowerShell user: Windows Terminal. It lets you use multiple PowerShell and other console tabs side-by-side, and you can mix Windows PowerShell, PowerShell 7, and Azure CloudShell consoles. You can install Windows Terminal from the Microsoft Store.

    As with any app, Windows Terminal is managed by Windows and can be updated at any time. It also is always installed…

    • 6 Nov 2020
  • Launching Windows Terminal for Any User

    On Windows 10, there is a new and awesome tool available for any PowerShell user: Windows Terminal. It lets you use multiple PowerShell and other console tabs side-by-side, and you can mix Windows PowerShell, PowerShell 7, and Azure Cloud Shell consoles. You can install Windows Terminal from the Microsoft Store.

    Since Windows Terminal is an „App“, it is always installed „per User“. You can launch it via its executable…

    • 4 Nov 2020
  • Permanently Deleting AD Objects

    Many Active Directory objects are protected from deletion. When you try to delete them, you get an error, preventing you from accidentally removing user accounts that can’t be restored.

    This of course prevents you from legit deletion or even moving objects to a new OU.

    To find out whether an AD object is protected from accidental deletion, use this:

    Get-ADObject DN of object -Properties Protecte…
    • 2 Nov 2020
  • Deploy PowerShell as Clickable Icons (Part 2)

    In the previous tip we illustrated how you can embed up to 4096 characters of PowerShell code inside a Windows Explorer shortcut file and produce clickable PowerShell code.

    The embedded PowerShell code can easily be viewed though simply by right-clicking the link file and opening the properties dialog.

    With a very simple adjustment, you can hide your payload though. Run the code below to produce a sample clickable PowerShell…

    • 29 Oct 2020
  • Deploy PowerShell as Clickable Icons (Part 1)

    You can use .lnk files to deploy small PowerShell solutions to end users. Here’s how:

    Take below code, and replace the payload code inside $code with whatever PowerShell code you’d like your clickable icon to execute. Just make sure the code is less than 4096 characters in total. Then run the script.

    $code = {
       # place your code here (must be less than 4096 characters)
       # (this example generates a battery…
    • 27 Oct 2020
  • Test-Driving New SSH Remoting

    If you’d like to test-drive the new PowerShell remoting alternative that is using SSH instead of WinRM, make sure you install PowerShell 7 first.

    Next, from within PowerShell 7, install this module:

     
    PS> Install-Module -Name Microsoft.PowerShell.RemotingTools -Scope CurrentUser  
     

    Once the module is installed, in an elevated PowerShell 7 shell, you can enable the new SSH-based remoting with just one call:

     
    • 23 Oct 2020
  • Test Whether Applications Exist

    Here is a simple one-liner that can test whether PowerShell 7 is installed on your system (or any other application):

    # name of application you want to test
    $name = 'pwsh'
    
    # try and find the application. Discard result and errors.
    Get-Command -Name $name -ErrorAction Ignore | Out-Null
    
    # if there was an error, the application does not exist
    $exists = $?
    
    
    # output result
    "Does $name exist? $exists"
    • 21 Oct 2020
  • Converting File Paths to 8.3 (Part 2)

    In the previous post we explained how you can use an old COM component to convert default long path names to short 8.3 path names. While that’s OK for occasional conversions, using COM components is slow and resource intense.

    A “cleaner” way would be to use Windows API calls directly. Here is how you can access the internal method that converts long file paths to short ones:

    # this is the long path to…
    • 19 Oct 2020
  • Converting File Paths to 8.3 (Part 1)

    Many years ago, file and folder names had a maximum of 8 characters, and these short path names still exist. They can even still be useful: short path names never contain spaces and other special characters and therefore never need to be quoted or escaped. Short paths can also be helpful when paths get very long.

    Yet how can you find out the short path name for a default long path name? One way is to use an old COM component…

    • 15 Oct 2020
  • Identifying PowerShell Host and Path

    Here is a quick one-liner that identifies the full path to your current PowerShell host:

     
    PS> (Get-Process -Id $pid).Path 
    C:\Program Files\PowerShell\7\pwsh.exe
     

    The path tells you where your current host is located, and you can check whether your code is being executed in Windows PowerShell, PowerShell 7, or the PowerShell ISE.

    In a similar approach, you can also find out the path of executables by name. For example…

    • 13 Oct 2020
  • Using Online Help (Part 2)

    In the previous tip we mentioned that many PowerShell users prefer the online help over locally downloaded help. To use the online help documents by default, try this in a fresh PowerShell console:

     
    # by default, -? opens LOCAL help
    PS> dir -?
    
    NAME
        Get-ChildItem
        
    SYNOPSIS
        Gets the items and child items in one or more specified locations.
        
        
    SYNTAX
    ...
    
    
    # with this line you tell PowerShell to use…
    • 9 Oct 2020
  • Using Online Help (Part 1)

    PowerShell supports both local help files and online resources. Take a look at the differences:

     
    # outputs help in same console window
    # level of detail depends on whether local help was
    # downloaded using Update-Help
    PS C:\> help -Name Get-Process
    
    NAME
        Get-Process
        
    SYNOPSIS
        Gets the processes that are running on the local computer or a remote computer.
        
        
    SYNTAX
        Get-Process [[-Name] <String[…
    • 7 Oct 2020
  • Updating Help without Admin Privileges

    In Windows PowerShell, updating help used to require Administrator privileges due to a design flaw: help had to be stored in the location where the modules resided. Updating help for Microsoft modules which are stored inside the Windows folder required write access to the Windows folder. That’s why normal users were unable to download and use local PowerShell help.

    In PowerShell 7, this design flaw has been corrected…

    • 5 Oct 2020
  • Checking Profile Scripts (Part 2)

    In the previous script we came up with a one-liner that checks which profile scripts actually exist. This solution works per host only, though, because each host uses its own host-specific profile paths.

    Here is a more generic approach: it lists all profile paths for all PowerShell hosts present on your system. You could then check these files as part of a security or sanity check:

    # calculate the parent paths that can…
    • 1 Oct 2020
  • Checking Profile Scripts (Part 1)

    PowerShell uses up to four profile scripts. When they exist, PowerShell silently executes any content when it launches.

    It’s important to know which profile script exist (if any) because their content can slow down PowerShell launch time, and they can be used by rouge code to sneak in.

    Testing for profile paths manually can be cumbersome. Here is a fun one-liner that does the job for you:

     
    $profile.PSObject.Pr…
    • 29 Sep 2020
  • Using Profile Scripts

    Profile scripts work like autostart scripts in PowerShell. They do not need to exist, but if they do, PowerShell executes its content silently during each launch. There are up to four profile scripts, and this line exposes their paths:

     
    PS> $profile | Select-Object -Property *
    
    AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
    AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell…
    • 25 Sep 2020
  • Identifying User Profile

    Be careful when using $env:userprofile or $home to create paths to user files. When a Windows box is set up for OneDrive, the documents folder may have been redirected to a subfolder named “OneDrive”. Here are some examples:

     
    PS> $env:USERPROFILE
    C:\Users\tobia
    
    PS> $HOME
    C:\Users\tobia
    
    PS> $profile
    C:\Users\tobia\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
    
    PS> 
     …
    • 23 Sep 2020
  • Get Rid of Get-EventLog

    The Get-EventLog cmdlet provides easy access to event log entries in the primary Windows event logs, however it neither can access the many application level event logs, nor is it available at all in PowerShell 7.

    If you ever plan to run your code in PowerShell 7, you should start getting used to its successor: Get-WinEvent. This cmdlet is powerful and supports many parameters. Here is an example that comes close to what…

    • 21 Sep 2020
  • Setting and Clearing Trusted Hosts

    PowerShell remoting maintains a list of trusted IP addresses and/or machine names on the client side (the machine that issues the command and authenticates at the server). This list is important for you because it governs how you can authenticate to remote computers.

    By default, PowerShell supports Kerberos authentication only because it is most secure and authenticates both sides, client and server. It requires an Active…

    • 17 Sep 2020
  • Using PowerShell 7 inside PowerShell ISE

    The PowerShell ISE built into Windows works with Windows PowerShell only and is stuck at PowerShell version 5.1. Typically, when you want to use an editor to write PowerShell 7 code, Visual Studio Code and the PowerShell extension is the way to go.

    Still, you can make PowerShell ISE “talk” to PowerShell 7. It then provides rich IntelliSense for PowerShell 7 and understands all language features that were introduced in…

    • 15 Sep 2020