Blog - Post List
    • 27 Jul 2017

    Power Tips: Easy Parsing of Setting Files (Part 2)

    In the previous tip we used ConvertFrom-StringData to parse plain text key-value pairs into hash tables. Here is an example where such conversion fails: $settings = @' Machine=Server12 Path=c:\test '@ $settings | ConvertFrom-StringData When you look at the result, you’ll quickly see why: Name Value ---- ----- Machine Server12 Path c: est Apparently, ConvertFrom-StringData treats “...
    • 26 Jul 2017

    Power Tips: Easy Parsing of Setting Files (Part 1)

    Let’s assume you want to save settings to a file in the most simplest form. Your settings may look like this: $settings = ' Name = Weltner FirstName = Tobias ID = 12 Country = Germany Conf = psconf.eu ' You could save these settings to file using Set-Content, and read them back using Get-Content. Yet how would you parse the information so that you can access individual items? There is...
    • 25 Jul 2017

    Power Tips: Adding Live Clock to PowerShell Title Bar (Part 2)

    In the previous tip we presented code that would update the PowerShell title bar in a background thread, displaying a live clock. Wouldn’t it be nice to also show the current path location? The challenge: how would the background thread know the current path of the foreground PowerShell? There is a PowerShell variable called $ExecutionContext which provides all kinds of useful information on the state of a context...
    • 24 Jul 2017

    Power Tips: Adding Live Clock to PowerShell Title Bar (Part 1)

    To continuously update the PowerShell title bar, and for example display the current date and time, you need a background thread that takes care of this. Without a background thread, your PowerShell would be busy all the time updating the title bar, thus unusable. Here is a simple piece of code you can run to display a live clock in your title bar: $code = { # submit the host process RawUI interface and the execution...
    • 21 Jul 2017

    Power Tips: When Add-Type Fails…

    Add-Type can be used to load additional .NET assemblies from DLL files into PowerShell. This works well most of the time, and here is a sample call (that would require the SharePoint DLL to be available of course): PS> Add-Type -Path "C:\SharepointCSMO\Microsoft.SharePoint.Client.dll" With some DLL files, this fails though, and PowerShell returns an exception saying “Unable to load one or more...
    • 20 Jul 2017

    Power Tips: Understanding PowerShell and System Paths

    PowerShell maintains its own current location: PS> Get-Location Path ---- C:\Users\tobwe The current location applies to relative paths used with any cmdlet. PS> Get-Location Path ---- C:\Users\tobwe PS> Resolve-Path -Path . Path ---- C:\Users\tobwe There is another current path, maintained by Windows, that applies to all .NET methods. It may be different than PowerShell’s current...
    • 19 Jul 2017

    Power Tips: Finding PowerShell’s Current File System Path

    To find out the path your PowerShell is currently using, simply run Get-Location: PS> Get-Location Path ---- C:\Users\tobwe However, the current path does not necessarily point to a files system location. If you changed location to the registry, for example, it looks like this: PS> cd hkcu:\ PS> Get-Location Path ---- HKCU:\ If you need to know the current file system path PowerShell uses...
    • 18 Jul 2017

    Power Tips: Dealing with “Windows PowerShell” and “PowerShell Core”

    There are two PowerShell Editions now: “Windows PowerShell” shipping with Windows, and running on the full .NET Framework, and the limited “PowerShell Core” running on .NET Core which is available cross-platform and runs on Nano Server, for example. Script authors targeting a specific PowerShell edition can now use the #requires statement to make sure their scripts run on the intended edition...
    • 17 Jul 2017

    Power Tips: Windows PowerShell and PowerShell Core

    Lately there has been confusion about PowerShell versions. There is a “PowerShell 6” open source initiative at GitHub (https://github.com/PowerShell/PowerShell). Does that mean the open source PowerShell 6 is the successor of PowerShell 5, and will eventually ship with Windows? No. There are just two distinct breeds of PowerShell now, so-called “PowerShell Editions”. “Windows PowerShell”...
    • 14 Jul 2017

    Power Tips: Setting PowerShell Title Text

    You probably know that you can change the title text of a PowerShell host window with a line like this: PS> $host . UI . RawUI . WindowTitle = "Hello World!" When you add this to your prompt function, the title text can be dynamic: function prompt { # get current path $path = Get-Location # get current time $date = Get-Date -Format ' dddd, MMMM dd ' # create title text $host ...
    • 13 Jul 2017

    Power Tips: Clearing All User Variables

    In the previous tip we illustrated how you can identify built-in PowerShell variables with an approach like this: $ps = [ PowerShell ] :: Create () $null = $ps . AddScript ( ' $null=$host;Get-Variable ' ) $ps . Invoke () $ps . Runspace . Close () $ps . Dispose () Now let’s do the opposite, and create a function that dumps only your own variables: function Get-UserVariable ( $Name = ' * '...
    • 12 Jul 2017

    Power Tips: Finding PowerShell Default Variables (Part 3)

    In the previous tip we illustrated how you can identify built-in PowerShell variables with an approach like this: $ps = [ PowerShell ] :: Create () $null = $ps . AddScript ( ' $null=$host;Get-Variable ' ) $ps . Invoke () $ps . Runspace . Close () $ps . Dispose () Apparently, this code still misses some variables that aren’t created by the PowerShell core engine, but instead are added later by...
    • 11 Jul 2017

    Power Tips: Finding PowerShell Default Variables (Part 2)

    In the previous tip we explained how you can use a separate new and fresh PowerShell to retrieve all default variables. When you examine these variables closely, you will discover that still some PowerShell variables are missing. Here is a slightly modified version called Get-BuiltInPSVariable that includes all reserved PowerShell variables: function Get - BuiltInPSVariable ( $Name = ' * ' ) { # create a...
    • 10 Jul 2017

    Power Tips: Finding PowerShell Default Variables (Part 1)

    Sometimes it would be useful to identify the automatic PowerShell variables managed by PowerShell so you could differentiate between built-in variables and your own. Get-Variable always dumps all variables. Here is a simple trick that uses a separate new and fresh PowerShell runspace to determine the built-in PowerShell variables: # create a new PowerShell $ps = [ PowerShell ] :: Create () # get all variables inside...
    • 7 Jul 2017

    Power Tips: Finding PowerShell Classes

    Starting in PowerShell 5, you can define PowerShell classes. They are defined dynamically and live in memory. So how would you know the names of classes that have been defined? Let’s first define a really simple class that does not do anything: class TestClass { } How would you be able to check whether a class named “TestClass” exists in memory? Here is a helper function called Get-PSClass...
    • 6 Jul 2017

    Power Tips: Using PowerShell Classes 2

    Starting in PowerShell 5, you can define PowerShell classes. You can use classes to create new objects, and by defining one or more “constructors”, you can easily initialize the newly created objects as well. Let’s have a look: class Employee { [ int ] $Id [ string ] $Name Employee ([ int ] $Id , [ string ] $Name ) { $this . Id = $Id $this . Name = $Name } Employee ([ string ] $Name...
    • 5 Jul 2017

    Power Tips: Using PowerShell Classes

    Starting with PowerShell 5, you can define classes. They have many use cases. One would be to create libraries of useful helper functions to better organize them. For this, a class would define “static” methods. Here is a simple example: class HelperStuff { # get first character of string and throw exception # when string is empty or multi-line static [ char ] GetFirstCharacter ([ string ] $Text ) ...
    • 4 Jul 2017

    Power Tips: Reading Registry Values (Workaround)

    In the previous tip we illustrated that Get-ItemProperty cannot read registry key values when there is a value present with corrupted content: PS> $key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0" PS> Get-ItemProperty -Path $key Get-ItemProperty : Specified cast is not valid. At line:1 char:1 ...
    • 3 Jul 2017

    Power Tips: Reading Registry Values Fails

    Occasionally, reading values of a registry key may fail with a strange error message: PS> $key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0" PS> Get-ItemProperty -Path $key Get-ItemProperty : Specified cast is not valid. At line:1 char:1 + Get-ItemProperty -Path $key + ~~~~~~~~~~~~~~~~~~~~~~~~~...
    • 30 Jun 2017

    Power Tips: Greetings of the Day (with Voice)

    In the previous tip we explained how you can add a personal greeting to your PowerShell profile. This greeting can also be spoken out, provided your volume is turned up. This works for all Powershell hosts including VSCode. This will add the code to your profile script: # create profile if it does not yet exist $exists = Test-Path -Path $Profile . CurrentUserAllHosts if ( ! $exists ) { $null = New-Item -Path...
    • 29 Jun 2017

    Power Tips: Greetings of the Day

    Here is a simple approach that takes an array of strings and returns a random string that you could use for custom greetings in PowerShell: $greetings = ' Hello there! ' , ' Glad to see you! ' , ' Happy coding! ' , ' Have a great day! ' , ' May the PowerShell be with you! ' $greetings | Get-Random All you need to do is add the code to your profile script, for...
    • 28 Jun 2017

    Power Tips: Truth about LINQ in PowerShell

    Lately there have been reports about using Linq, a .NET query language, with PowerShell in an effort to speed up code. Until there is true Linq support in PowerShell, using Linq is very cumbersome and requires strict typing and access to undocumented methods. What’s more, the same can be achieved with pure PowerShell approaches, and the speed increase often is just marginal – at least not very relevant to...
    • 27 Jun 2017

    Power Tips: Secret Windows 10 Transparency Mode

    When you open a native PowerShell console in Windows 10, you can hold down CTRL+SHIFT, then move your mouse wheel, to adjust console background color transparency, and let other windows shine through. The same works for cmd.exe as well, of course. ReTweet this Tip!
    • 26 Jun 2017

    Power Tips: Flush DNS Cache

    Windows is using a DNS cache, so when you change DNS servers, you need to flush the cache before the new settings take effect. PowerShell is a friend with old console commands, so you can simply run this line in PowerShell: PS> ipconfig /flushdns ReTweet this Tip!
    • 23 Jun 2017

    Power Tips: Displaying Folder Tree

    PowerShell is a friend with old console commands, so the easiest way of displaying the tree structure of a folder is the old “tree” command. It works best in a native PowerShell console because editors often use a different character set. Try this: PS> Tree $home Just make sure you are running this command in a native PowerShell console or VSCode. You can then pipe the result to clip.exe and paste...