Extending Robocopy

PowerShell can add value to existing commands such as robocopy. Take a look at the below function–it uses robocopy to copy files, and adds the ability to perform a „Flat Copy“ as well as the option to open the destination folder after the copy is done:

#requires -Version 3.0

function Copy-FileWithRobocopy
    [string]$Filter = '*',
    [int]$RetryCount = 0,
    [string]$ExcludeDirectory = '',
  $Recurse = '/S'
  if ($NoRecurse) { $Recurse = '' }
  robocopy.exe $Source $Destination $Filter /R:$RetryCount $Recurse /XD $ExcludeDirectory
  if ($FlatCopy)
    Get-ChildItem -Path $Destination -Recurse -Filter $Filter | 
      Move-Item -Destination $Destination -Force
    Get-ChildItem -Path $Destination -Directory | 
      Remove-Item -Recurse -Force
  if ($Open)
    explorer $Destination

This would copy all log files from any subfolder inside the Windows folder to a new folder named c:logs, and performs a flat copy:

PS>  Copy-FileWithRobocopy -Source $env:windir -Destination c:logs -Filter *.log -FlatCopy -Open

Before you use this on production systems, take a look at how –FlatCopy works: it simply looks for any files inside the destination folder that match the specified filter, and moves them to the root, then deletes all folders.

So duplicate files will be overwritten, and if the destination folder contained other data before in subfolders, these will be deleted. It’s a very simple approach that works for many use cases but leaves a lot of room for improvement.

Twitter This Tip! ReTweet this Tip!

Copy Color-Coded Code

When you select code in the PowerShell ISE and copy it to the clipboard, it is copied in RTF format and preserves all color coding and font information. You can paste it in RTF-aware applications such as Word, and receive nicely formatted and color-coded PowerShell code.

To adjust the font size, you cannot use the slider in the lower right-hand side of the PowerShell ISE. This slider just scales the font inside the PowerShell ISE but does not affect the font size of copied code.

Instead, in the PowerShell ISE, choose Tools/Options, then adjust the font size in the Options dialog

Twitter This Tip! ReTweet this Tip!

Creating WinForms GUIs in PowerShell

While it is recommended to use the modern WPF technology to create PowerShell user interfaces, you might still want to occasionally use the older WinForms technique, especially if you need to target machines without .NET framework 3.51 or better. WinForms user interfaces require a lot of code, and it’s not particular intuitive.

So here is a URL to a free graphical PowerShell online editor that creates the code for you on the fly: http://www.poshgui.com/

Twitter This Tip! ReTweet this Tip!

Using “Exit” to Communicate with Linux

When a PowerShell script ends, you can run the command “Exit” and submit a numeric value. This has been good practice in the Windows world to set the “Error Level” that can be read by the caller (for example, a batch file or the scheduled task manager).

exit 99

Now that PowerShell is available on Linux as well, it can also be used to report back a status number to the calling Linux process.

Twitter This Tip! ReTweet this Tip!

Capturing Linux Output

If you run PowerShell on Linux, you can combine Linux and PowerShell commands. To take the output of a Linux command and assign it to a PowerShell variable, do something like this:

$content = (ls)

Note that “ls” is an alias on Windows systems but refers to the original ls command on Linux systems.

Twitter This Tip! ReTweet this Tip!

Test for File or Folder

Test-Path can check whether a file or folder exists. If you add -PathType and specify Leaf (for files) or Container (for folders), the result can be even more specific:

$path = 'c:windows'

Test-Path -Path $path
Test-Path -Path $path -PathType Leaf
Test-Path -Path $path -PathType Container

Twitter This Tip! ReTweet this Tip!