Blog - Post List
    • 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...
    • 22 Jun 2017

    Power Tips: Using Cached Port File

    In the previous tip we explained how you can download port assignments via PowerShell from IANA. This process requires Internet access and can take a while. So here is code that looks for a cached CSV file. If it is present, port data is loaded from file offline, else the data is retrieved online, and a cache file is written. Note especially how Tee-Object is used to create the cache file: $url = ' https://www.iana...
    • 21 Jun 2017

    Power Tips: Get List of Port Assignments

    The IANA (Internet Assigned Numbers Authority) maintains a CSV file with all known port assignments. PowerShell can download this list for you: $out = " $env:temp\portlist.csv " $url = ' ' $web = Invoke-WebRequest -Uri $url -UseBasicParsing -OutFile $out Import-Csv -Path $out -Encoding UTF8 The result...
    • 20 Jun 2017

    Power Tips: World Time Clock

    PowerShell 5 comes with Get-TimeZone which returns all defined time zones and their time offset. This is all you need for a one-liner world clock: $isSummer = ( Get-Date ) . IsDaylightSavingTime () Get-TimeZone -ListAvailable | ForEach-Object { $dateTime = [ DateTime ] :: UtcNow + $_ . BaseUtcOffset $cities = $_ . DisplayName . Split ( ' ) ' )[ - 1 ] . Trim () if ( $isSummer -and $_ . SupportsDaylightSavingTime...
    • 19 Jun 2017

    Power Tips: Check for Daylight Savings Time

    Here is how PowerShell can find out whether Daylight Savings Time is currently effective – a potentially needed detail when doing GMT calculations: ( Get-Date ) . IsDaylightSavingTime () ReTweet this Tip!
    • 16 Jun 2017

    Power Tips: Setting Time Zone

    While you need administrative privileges to adjust time and date on your computer, each user can change the time zone, i.e. when you travel. PowerShell 5 comes with a very simple family of cmdlets to manage time zones. First, check out your current setting: PS> Get-TimeZone Id : W. Europe Standard Time DisplayName : (UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien StandardName : Mitteleuropäische...
    • 15 Jun 2017

    Power Tips: Finding All Variables in a Script

    In the previous tip we illustrated how you can analyze the content of a script block and search for variables or commands. The same is possible for text-based scripts. The script below will analyze itself and dump variables and commands: $filepath = $PSCommandPath $tokens = $errors = $null $ast = [ System.Management.Automation.Language.Parser ] :: ParseFile ( $filepath , [ ref ] $tokens , [ ref ] $errors ) #...
    • 14 Jun 2017

    Power Tips: Finding All Variables in a Script Block

    To analyze the content of a script block, you can easily examine the AST, and, for example, create a list of all variables in the code: $code = { $a = "Test" $b = 12 Get-Service Get-Process $berta = 100 } $code . Ast . FindAll ( { $true } , $true ) | Where-Object { $_ . GetType () . Name -eq ' VariableExpressionAst ' } | Select-Object -Property VariablePath -ExpandProperty Extent...
    • 13 Jun 2017

    Power Tips: Running PowerShell Script as a Scheduled Task

    If you need to run a PowerShell script in regular intervals, why not run it as a scheduled task? Here are some lines that help you create a new scheduled task to run a PowerShell script at 6AM: #requires -Modules ScheduledTasks #requires -Version 3.0 #requires -RunAsAdministrator $TaskName = ' RunPSScriptAt6 ' $User = "train\tweltner" $scriptPath = "\\Server01\Scripts\find-newaduser.ps1"...
    • 12 Jun 2017

    Power Tips: Spying on Function Source Code

    Here is a quick way how to find the source code of PowerShell functions: $ { function : Clear - Host } | clip This would copy the Clear-Host source code to the clipboard, and when you paste it, you’ll see how Clear-Host works: $RawUI = $Host . UI . RawUI $RawUI . CursorPosition = @ { X = 0 ;Y = 0 } $RawUI . SetBufferContents ( @ { Top = -1 ; Bottom = -1 ; Right = -1 ; Left = -1 } , @ { Character =...
    • 9 Jun 2017

    Power Tips: Modern Alternative to More

    In a PowerShell console, you can continue to pipe to more, just like in cmd.exe, to view results page by page. However, more does not support real-time pipelining, so all data needs to be collected first. This can take a long time and burn much memory: dir c:\windows -Recurse -ea 0 | more A better way is to use PowerShell’s own paging mechanism: dir c:\windows -Recurse -ea 0 | Out-Host -Paging Note that...
    • 8 Jun 2017

    Power Tips: Creating SMB Shares Remotely

    Here are a couple of lines that remotely create an SMB share on a server: #requires -Version 3.0 -Modules CimCmdlets, SmbShare -RunAsAdministrator $computername = ' Server12 ' $shareName = ' ScriptExchange ' $fullAccess = ' domain\groupName ' $session = New-CimSession -ComputerName $computername New-SMBShare -Name $shareName -Path c:\Scripts -FullAccess $fullAccess -CimSession $session...
    • 7 Jun 2017

    Power Tips: Important PowerShell Variables

    Here is a list of important PowerShell variables: $pshome is the path to the place where PowerShell lives. $home is the path to your personal profile folder. And $PSVersionTable returns the PowerShell version and the versions of important subcomponents: PS> $pshome C:\Windows\System32\WindowsPowerShell\v1.0 PS> $HOME C:\Users\tweltner PS> $PSVersionTable Name Value ---- ----- PSVersion 5.1.14393.0 PSEdition...
    • 6 Jun 2017

    Power Tips: Read-Host Blocks Automation

    Using Read-Host to ask for user information can be problematic because it prevents scripts from running unattended. A better way could be to wrap Read-Host in the param() block. This way, the information can be submitted via arguments for unattended operation, and prompted for interactive usage: param ( $Name = $ ( Read-Host -Prompt ' Enter your name ' ) , $Id = $ ( Read-Host -Prompt ' Enter your ID...
    • 5 Jun 2017

    Power Tips: Force Client Time Resync

    If your client does not sync time correctly with your domain controller, try the code below. It does require Admin privileges: w32tm.exe /resync /force ReTweet this Tip!
    • 2 Jun 2017

    Power Tips: Mapping Network Drives

    PowerShell offers numerous ways to connect to SMB file shares. Here are three different approaches: # adjust path to point to your file share $UNCPath = ' \\server\share ' net use * $UNCPath New-PSDrive -Name y -PSProvider FileSystem -Root $UNCPath -Persist New-SmbMapping -LocalPath ' x: ' -RemotePath $UNCPath Net.exe is the most versatile approach and available in all PowerShell versions...
    • 1 Jun 2017

    Power Tips: Safely Deleting Data

    To safely delete files, folders, or entire drives, PowerShell can use the built-in cipher.exe tool. This line would safely delete the old user profile: Cipher.exe / w : c:\Users\ObsoleteUser Note that the path to the folder to delete needs to be separated by a *** from the parameter /w. Clearing the data takes a while: Windows is overwriting the entire data content multiple times to ensure it cannot be recovered...
    • 31 May 2017

    Power Tips: Finding Organizational Units

    Get-OrganizationalUnit (from Microsofts free RSAT tools) can search for organizational units based on fully distinguished name or GUID, or you can use the –Filter parameter. Unfortunately, -Filter cannot easily be automated. The code below does not work and will not return all organizational units with “Test” in their name: $Name = ' Test ' Get-ADOrganizationalUnit -Filter { Name -like...
    • 30 May 2017

    Power Tips: Testing Organizational Unit

    Provided you have installed the free Microsoft RSAT tools, here is a simple way to check whether an OU exists: $OUPath = ' OU=TestOU,DC=train,DC=powershell,DC=local ' $exists = $ ( try { Get-ADOrganizationalUnit -Identity $OUPath -ErrorAction Ignore } catch {}) -ne $null "$OUPath : $exists" $exists will be $true or $false, indicating whether the OU was found. Note the use of try/catch error...
    • 29 May 2017

    Power Tips: Validating Variables

    Variables and function parameters can be automatically validated through validation attributes. Here is a simple example making sure $test1 can only store values between 1 and 10: [ ValidateRange ( 1 , 10 )] $test1 = 10 Once you assign a value less than 1 or greater than 10 to it, PowerShell throws an exception. You just don’t have any control over the exception text. By using a script validator, you can...
    • 26 May 2017

    Power Tips: Cloning DHCP Server Settings

    Beginning with Windows Server 2012, you can easily export and re-import DHCP settings. Cloning or migrating DHCP servers is a snap. The example below exports the settings from \\ORIGDHCP and imports them into the local DHCP Server: Export-DHCPServer -File " $env:temp\dhcpsettings.xml " -Computername ORIGDHCP Import-DHCPServer -File " $env:temp\dhcpsettings.xml " ReTweet this Tip!
    • 25 May 2017

    Power Tips: Searching for ADUsers

    The free Microsoft RSAT tools come with the PowerShell “ActiveDirectory” module: plenty of cmdlets help you administer Active Directory users and computers. One cmdlet is especially useful. Rather than trying to find AD users with Get-ADUser and complex filters, use Search-ADAccount instead. It is specialized in finding users based on common scenarios. This line, for example, finds all user accounts that...
    • 24 May 2017

    Power Tips: Generating Random Passwords

    Here is a very simple way to create complex random passwords: Add-Type -AssemblyName System.Web $PasswordLength = 12 $SpecialCharCount = 3 [ System.Web.Security.Membership ] :: GeneratePassword ( $PasswordLength , $SpecialCharCount ) The API call lets you choose the length of the password, and the number of non-alphanumeric characters it contains. ReTweet this Tip!
    • 23 May 2017

    Power Tips: ToString() Masquerade

    In the previous tip we explained that ToString() is a fuzzy way of describing an object, and that the object author can decide what ToString() returns. This is especially true for your PowerShell code. Have a look how easily you can overwrite ToString() for any object: PS> $a = 1 PS> $a | Add-Member -MemberType ScriptMethod -Name toString -Value { 'go away' } -Force PS> $a go away PS> $a.GetType...