Blog - Post List
    • 28 Apr 2017

    Power Tips: Cloning Folder Structures (with NTFS Permissions) – Part 2

    In the previous tip we illustrated how Get-FolderStructureWithPermission can document and create a list of all nested folders in a structure, along with their respective NTFS security settings. The result could be saved in a variable, or serialized to disk using Export-Csv Today we show you the second part: once you have the information about a given folder structure, here is Set-FolderStructureWithPermission. It accepts...
    • 27 Apr 2017

    Power Tips: Cloning Folder Structures (with NTFS Permissions) – Part 1

    Sometimes you need to re-create a nested folder structure, and may also want to clone the NTFS permissions. Today we are tackling the first step: recording an existing folder structure along with NTFS permissions in SDDL form. This task is accomplished by Get-FolderStructureWithPermission. It accepts a path to an existing folder, then returns all of its child folders along with their NTFS permissions in SDDL form: ...
    • 26 Apr 2017

    Power Tips: Evaluating Exit Codes (aka Error Level – Part 3)

    In part 3 of our mini-series about running console applications in PowerShell, here is a goodie: how can you run a console application separately from PowerShell, and still get notified when it is done, and retrieve its exit code? Here is how: the code below runs ping.exe in a separate (hidden) window. PowerShell continues and is free to do whatever it wants. In the example, it outputs a number of “dot”...
    • 25 Apr 2017

    Power Tips: Evaluating Exit Codes (aka Error Level – Part 2)

    When you directly launch a console-based application, PowerShell returns its exit code (aka Error Level) in the automatic variable $LASTEXITCODE. However, how do you get the exit code for a console-based application that you launch via Start-Process? Here is how: $hostname = ' powershellmagazine.com ' # run the console-based application synchronously in the PowerShell window, # and return the process object...
    • 24 Apr 2017

    Power Tips: Evaluating Exit Codes (aka Error Level – Part 1)

    When running a console-based application, it typically returns a numeric exit code. The meaning of this exit code is up to the console-based application, and you’d have to look it up there. PowerShell does hand you the exit code, though. It surfaces in $LASTEXITCODE. Here is an example using ping.exe to test for network response: $hostname = ' powershellmagazine.com ' # run console-based executable...
    • 21 Apr 2017

    Power Tips: Ejecting CD Drive

    Here is a fun little function that uses WMI to eject your CD drive. It does so by first asking WMI for all CD drives. It then uses the explorer object model to navigate to the drive and call its context menu item “Eject”: function Eject-CD { $drives = Get-WmiObject Win32_Volume -Filter "DriveType=5" if ( $drives -eq $null ) { Write-Warning "Your computer has no CD drives to eject."...
    • 20 Apr 2017

    Power Tips: Identifying CSV Delimiter

    When importing a CSV file with Import-Csv, you need to specify a delimiter, and if you pick the wrong one, import obviously fails. You need to know the delimiter a CSV file is using. Here is a simple approach that tells you the delimiter for a given CSV file: function Get - CsvDelimiter ( $Path ) { # get the header line $headerLine = Get-Content $Path | Select-Object -First 1 # examine header line per character...
    • 19 Apr 2017

    Power Tips: Identifying Duplicate CSV Headers (Part 2)

    When a CSV file contains duplicate column headers, it cannot be imported. In the previous tip we illustrated how you can detect duplicate column headers in a CSV file. Now here is an approach that auto-corrects duplicate entries. First, you need a CSV file with duplicate column headers. On German systems, for example, you could create one like this: PS C:\> driverquery /V /FO CSV | Set-Content -Path $env:temp...
    • 18 Apr 2017

    Power Tips: Identifying Duplicate CSV Headers (Part 1)

    CSV files are just text files, so it is easy to strip the first line and examine its headers. If you don’t have a CSV file at hand, here is a simple line to create one to play with: PS C:\> Get-Process | Export-Csv -Path $env:temp\test.csv -NoTypeInformation -Encoding UTF8 -UseCulture PS C:\> Now you can analyze its headers. This simple approach tells you whether there are duplicate headers in a CSV...
    • 17 Apr 2017

    Power Tips: Serializing Date and Time in a Culture-Invariant Way

    When you save date and time to text, for example when exporting to CSV, or when creating text reports, DateTime objects are converted to the date and time format defined in your regional settings: PS> $date = Get-Date -Date '2017-02-03 19:22:11' PS> "$date" 02/03/2017 19:22:11 PS> $date.ToString() 03.02.2017 19:22:11 PS> Get-Date -Date $date -DisplayHint DateTime Freitag, 3. Februar...
    • 14 Apr 2017

    Power Tips: Converting Ticks to Date and Time (Part 2)

    In the previous tip we explained how you convert date and time displayed as ticks to a true DateTime format. However, there are two different time formats involving ticks, and here is an overview of how you can convert numeric datetime information: PS> $date = Get-Date -Date '2017-02-03 19:22:11' PS> $ticks = $date.Ticks PS> $ticks 636217465310000000 PS> [DateTime]$ticks Friday, February 3,...
    • 13 Apr 2017

    Power Tips: Converting Ticks to Date and Time (Part 1)

    Occasionally, you may run into strange date and time representations: they might be represented as a 64-bit integer like this: 636264671350358729. If you’d like to convert these “ticks” (the smallest time and date increment on Windows), simply convert the number to a DateTime type: PS> [DateTime]636264671350358729 Thursday, March 30, 2017 10:38:55 Likewise, to turn a date into ticks, try...
    • 12 Apr 2017

    Power Tips: Directly Importing Certificates (Part 2)

    In the previous tip we showed how to import digital certificates with .NET methods on any version of PowerShell. Newer versions of PowerShell come with the “PKI” module which includes the cmdlet Import-Certificate. With it, importing certificates becomes even easier. #requires -Version 2.0 -Modules PKI # importing to personal store $Path = ' C:\Path\To\CertFile.cer ' Import-Certificate -FilePath...
    • 11 Apr 2017

    Power Tips: Directly Importing Certificates (Part 1)

    Installing certificate files on a computer can be done using .NET methods with any version of PowerShell. This would import a certificate file to your personal store: # importing to personal store $Path = ' C:\Path\To\CertFile.cer ' $Store = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList My , CurrentUser $Store . Open ( ' ReadWrite ' ) $Store . Add (...
    • 10 Apr 2017

    Power Tips: Beware of Aliases

    Can you spot what is wrong here? PS C:\> function r { "This never runs" } PS C:\> r function r { "This never runs" } PS C:\> When you run function “r”, it simply returns the function source code. The reason is that the function name “r” conflicts with a built-in alias: PS C:\> Get-Alias r CommandType Name Version Source ----------- ---- ------- ---...
    • 7 Apr 2017

    Power Tips: Dealing with Long File Paths

    Historically, the Windows file system sometimes stalls when paths grow longer than 256 characters. There is a module available at the PowerShell Gallery that adds cmdlets to quickly search the file system, and support paths of any length. If you use PowerShell 5 or have installed PowerShellGet ( www.powershellgallery.com ), this is how you can download and install the “PSAlphaFS” module from the PowerShell...
    • 6 Apr 2017

    Power Tips: Detecting Character Code 0

    Occasionally, strings use a “Byte 0” character as a delimiter. Unlike most other delimiters, this delimiter does not show in text output but can still be used to separate the text parts. PowerShell can deal with character code 0 strings. It is represented by a backtick followed by the number 0. Note that text needs to be placed in double-quotes in order to convert the backtick sequence to byte 0. Here...
    • 5 Apr 2017

    Power Tips: Auto-Declaring Alias Names for Functions

    You probably know that PowerShell supports alias names for commands. But did you know that you can define alias names for PowerShell functions inside a function definition (introduced in PowerShell 4)? Have a look: function Get-AlcoholicBeverage { [ Alias ( ' Beer ' , ' Drink ' )] [ CmdletBinding ()] param () "Here is your beer." } The “official” name for the function...
    • 4 Apr 2017

    Power Tips: Checking Operating System Version

    Here is a simple and fast way of checking the operating system version: PS C:\> [Environment]::OSVersion Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT 10.0.14393.0 Microsoft Windows NT 10.0.14393.0 So now it’s a snap checking whether a script runs on the intended operating system. To check for Windows 10, for example, try this: PS C:\> [Environment...
    • 3 Apr 2017

    Power Tips: Comparing Against $NULL

    If you want to find out whether a variable contains $Null (nothing), always make sure you keep $null on the left side of the comparison. Most of the time, the order does not really matter: PS C:\> $a = $null PS C:\> $b = 12 PS C:\> $a -eq $null True PS C:\> $b -eq $null False However, if a variable contains an array, placing the array on the left side of the comparison operator makes it work like...
    • 31 Mar 2017

    Power Tips: Examining Certificate Details

    If you’d like to examine and view the details of a certificate file without the need to import it into your certificate store, here is a simple example: # replace path with actual path to CER file $Path = ' C:\Path\To\CertificateFile\test.cer ' Add-Type -AssemblyName System.Security [ Security.Cryptography.X509Certificates.X509Certificate2 ] $cert = [ Security.Cryptography.X509Certificates.X509Certificate2...
    • 30 Mar 2017

    Power Tips: Exporting ActiveDirectory Module

    To manage users and computers in your Active Directory from PowerShell, you need the ActiveDirectory module which comes as part of the free RSAT tools from Microsoft. Provided you are domain administrator and have remoting access to your domain controller, you can also export the ActiveDirectory module from your DC, and use it locally via implicit remoting. Here is how you do this: $DC = ' dc1 ' # rename...
    • 29 Mar 2017

    Power Tips: Managing NTFS Permissions

    While there are not many built-in cmdlets to manage NTFS permissions, there is a growing list of open source PowerShell modules adding these. One promising module is written by Raimund Andree, a German Microsoft engineer who will also speak at the upcoming PowerShell Conference EU ( www.psconf.eu ). If you use PowerShell 5 or have installed PowerShellGet ( www.powershellgallery.com ), this is how you can download and...
    • 28 Mar 2017

    Power Tips: Determine if Array Contains Value – Using Wildcards

    If you’d like to know whether an array contains a given element, PowerShell provides the –contains operator. This operator does not support wildcards, though, so you only can check for exact matches. Here is a workaround that helps you filter array elements with wildcards: $a = ' Hanover ' , ' Hamburg ' , ' Vienna ' , ' Zurich ' # is the exact phrase present in array...
    • 27 Mar 2017

    Power Tips: Working with LDAP and Dates

    LDAP filters are a fast and powerful way of retrieving information from Active Directory. However, LDAP filters use a very low-level date and time format. It is basically a huge integer number. Fortunately, PowerShell contains ways of converting real DateTime objects into these numbers, and vice versa. Here is a code sample that uses Get-ADUser from ActiveDirectory module to find all users who recently changed their...