Category Archives: Powershell

Java UTF-16LE Base64 CODEC

The UTF-16LE base64 encoding is compatible to be used with powershell's encoded command.


//The text to encode.
var command = "Write-Output \"Hello World\"";
var encodedString = Base64.getEncoder().encodeToString(command.getBytes(StandardCharsets.UTF_16LE));
System.out.printf("Base64: %s%n", encodedString);



The preceding output can be used with powershell like the following:

powershell -encodedcommand VwByAGkAdABlAC0ATwB1AHQAcAB1AHQAIAAiAEgAZQBsAGwAbwAgAFcAbwByAGwAZAAiAA==


//The base64 text to decode.
byte[] decodedBytes = Base64.getDecoder().decode(base64);
String decodedString = new String(decodedBytes, StandardCharsets.UTF_16LE);
System.out.printf("Decoded: %s%n", decodedString);


Decoded: Write-Output "Hello World"

Removing the Timestamp from the downloaded SNAPSHOT

Use case

The downloaded snapshot has timestamp associated with it like the following:


But the tooling is expecting an absolute name like the the following:


Powershell Script

#The target artifact
$ArtifactId = "artifact"

#The target SNAPSHOT version
$Version = "1.0.0-SNAPSHOT"

if ($Version -match "^(.*)-SNAPSHOT$") 
    $Prefix = "{0}-{1}" -f $ArtifactId,$Matches.1
    $Pattern = "^(${Prefix}).*(\.jar)$"

    Get-ChildItem ('.') | ForEach-Object {
        If ($_.Name -match $Pattern) {
            $NewName = "{0}-SNAPSHOT{1}" -f $Matches.1, $Matches.2
            Rename-Item $_ -NewName $NewName
            $Message = "Renaming from {0} to {1}" -f $_.Name, $NewName
            echo $Message

Sample Show Balloon Notification for PowerShell

A sample implementation of windows balloon notification for powershell is as follows:

Add-Type -AssemblyName System.Windows.Forms

function FnShowBalloon {

           [string] $icon = 'info', 
           [int] $delay = 20000, 
           [int] $sleep=0)

    Switch($icon.ToString().ToLower()) {
        'warn' {$iconInstance = [System.Windows.Forms.ToolTipIcon]::Warning}
        'error' {$iconInstance = [System.Windows.Forms.ToolTipIcon]::Error}
        'info' {$iconInstance = [System.Windows.Forms.ToolTipIcon]::Info}
        default {$iconInstance = [System.Windows.Forms.ToolTipIcon]::None}

    $notification = New-Object System.Windows.Forms.NotifyIcon
    $path = (Get-Process -id $pid).Path
    $notification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
    $notification.BalloonTipIcon = $iconInstance
    $notification.BalloonTipTitle = $title
    $notification.BalloonTipText = $message
    $notification.Visible = $true

    if ($sleep -gt 0) {
        Start-Sleep -s $sleep

The FnShowBalloon  function can be used as follows:

FnShowBalloon -title "Hello World" -message "This is a sample message" -icon info

Recommended Way to Redirect the Output to a Text File in PowerShell

Instead of using > to redirect the output into a file, pipe it to out-file cmdlet.

The out-file cmdlet allows some useful parameters as follows:

Parameter Argument Description
-Append Adds the output to the end of an existing file.
-Encoding Encoding Specifies the type of encoding for the target file. The default value is utf8NoBOM.

The acceptable values for this parameter are as follows:

ascii: Uses the encoding for the ASCII (7-bit) character set.
bigendianunicode: Encodes in UTF-16 format using the big-endian byte order.
oem: Uses the default encoding for MS-DOS and console programs.
unicode: Encodes in UTF-16 format using the little-endian byte order.
utf7: Encodes in UTF-7 format.
utf8: Encodes in UTF-8 format.
utf8BOM: Encodes in UTF-8 format with Byte Order Mark (BOM)
utf8NoBOM: Encodes in UTF-8 format without Byte Order Mark (BOM)
utf32: Encodes in UTF-32 format.

-FilePath Path Specifies the path to the output file.

To redirecting the output of the dir command to dir.txt file, use the following command:

dir | out-file -encoding ascii -filepath "dir.txt"

Enable Windows Subsystem for Linux (WSL) and Install Linux Distribution

If you want to install any Linux distributions for WSL ensure that the Microsoft-Windows-Subsystem-Linux optional feature is enabled using the following procedure:

  1. Open a PowerShell as Administrator and run the following (i.e. if it is not installed):
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  2. Restart the computer when prompted.
  3. Open the Microsoft Store
  4. Search for the Linux distribution (e.g. Ubuntu) you like.
  5. Click it and the click the Install button.
  6. After finishing the installation search for it in the Start menu.

    Usually the newly installed application is at the top of the start menu.

Make the Target Folder always in-sync from the Source Folder using Beyond Compare

  1. Create a folder compare session with beyond compare like the following:
  2. Save the session with a file name without any special characters (e.g. < or >) or spaces (i.e. necessary not to confuse the scheduler).
  3. Save the following beyond compare script to right-updater.bc file.
    #Write to right-updater.log
    log normal "right-updater.log"
    #Load a folder compare session.
    load "%1"
    #Confirmation will be set to yes by default. 
    option confirm:yes-to-all
    #Make the right equals to the left of the loaded folder from the session.
    sync mirror:left->right
  4. Save the following powershell script to bcrunner.ps1 file along the side right-updater.bc file.
    #expects 2 command line parameters
    param([string]$script, [string]$session)
    #The beyond compare command to be used.
    $cmd="C:\Program Files\Beyond Compare 4\"
    #Holds the session argument for the beyond compare command.
    #Write on screen the actual command to be executed.
    write-output "$cmd /silent @$script $args"
    #Perform the command.
    &$cmd /silent @$script $args
  5. Schedule using windows task scheduler and on the actions tab fill in the following fields like:
    Action Start a program
    Program/script powershell
    Add arguments(optional) ./bcrunner.ps1 right-updater.bc <SAVED_SESSION_FROM_STEP_2>
    Start in (optional) <DIRECTORY_OF_BCRUNNER.PS1_FILE_FROM_STEP_4>

    Note: Fill-in the other tabs of the scheduler accordingly.