Using PowerShell Core on Windows, Linux and macOS

Windows PowerShell is nothing new. It has been along with Windows for many years. Honestly, I preferred to use CMD more than PowerShell due to the learning cost. In the past few years, under the leadership of Satya Nadella, Microsoft is becoming opened and creative. More and more tools and frameworks support Windows, Linux, and macOS, including Visual Studio Code, .NET Core, and PowerShell. It is so good to use one scripting language for all platforms.

Command Prompt vs. PowerShell

For someone who is familiar with Linux Shell, Command Prompt as a Windows command line tool is probably a nightmare. Let’s try some simple commands to compare Command Prompt and PowerShell on Windows.

Change directory

PowerShell versus CMD

The ‘cd’ command cannot work for changing drives in Command Prompt.

Home

PowerShell home

We can go to the home directory with ‘~’ symbol in PowerShell.

PowerShell supports a variety of commands that are used on Windows, Linux, and macOS. There are no obstacles to get used to PowerShell. You don’t need to change your habit. PowerShell is similar to Linux Shell but better. I still remember the day that Linux Shell syntax made me a headache. With PowerShell, I can quickly write scripts for Linux and macOS.

PowerShell Tricks

How to show top 5 processes?

Get-Process | Select-Object -First 5

How to convert output to CSV and HTML files?

Get-Process | Select-Object -First 5 | ConvertTo-Csv | Out-File process.csv
Get-Process | Select-Object -First 5 | ConvertTo-Html | Out-File process.html

How to modify the suffixes of multiple files? For example, rename all of the .tpl files in the current directory to .json.

Get-ChildItem *.tpl | Rename-Item -NewName { $_.name -Replace '\.tpl$','.json' }

How to color the console output?

Write-Host "This line is yellow” -Fore Yellow

PowerShell color output

How to run multiple jobs and check job status?

start-job -ScriptBlock {Get-Service | Select-Object -First 5 | ConvertTo-Html | Out-File g:\service.html}
start-job -ScriptBlock {Get-Process | Select-Object -First 5 | ConvertTo-Html | Out-File g:\process.html}
Get-Job

How to define an array variable?

$v = '1', '2', '3'
Write-Output $v[0]
Write-Output $v[1]
Write-Output $v[2]

How to reads a line of input from the console?

$name = Read-Host "Enter a name"
[int]$number = Read-Host "Enter a number"

Automation Demo: Building Barcode Reader Project

Prerequisites

Basic ideas

  1. Select your operating system.
  2. Get the source code of barcode reader app.
  3. Set the absolute path of your barcode SDK.
  4. Build the project.
  5. Run the app.
# Select the platform.
Write-Host "Please select the number for your operating system." -Fore Yellow 
$os = Read-Host "1. Windows 2. Linux 3. macOS"

# Get the source code.
Write-Host "Downloading the source code..." -Fore Yellow 
$sourceURL = "https://github.com/dynamsoft-dbr/cmake.git"
git clone $sourceURL

if ($os -eq "1") {
    Write-Output "Configuring Windows..."

    # Set the library path.
    $libPath = Read-Host "Please add the .lib file path"
    $dllPath = Read-Host "Please add the .dll file path"

    # Copy library files to the destination folder.
    Copy-Item $libPath .\cmake\platforms\win\
    Copy-Item $dllPath .\cmake\platforms\win\

    # Build the project.
    Write-Host "Building the project..." -Fore Yellow 

    if ($libPath.Contains("x86.lib")) {
        New-Item -Path ".\cmake\build" -ItemType "directory"
        Set-Location -Path ".\cmake\build"
        cmake ..
        cmake --build .
    }
    else {
        New-Item -Path ".\cmake\build64" -ItemType "directory"
        Set-Location -Path ".\cmake\build64"
        cmake -G"Visual Studio 14 2015 Win64" ..
        cmake --build .
    }

    # Run the project.
    Write-Host "Testing the application..." -Fore Yellow 
    .\Debug\BarcodeReader.exe
}
elseif ($os -eq "2") {
    Write-Output "Configuring Linux..."

    # Set the library path.
    $soPath = Read-Host "Please add the .so file path"

    # Copy library files to the destination folder.
    Copy-Item $soPath ./cmake/platforms/linux

    # Build the project.
    New-Item -Path "./cmake/build" -ItemType "directory"
    Set-Location -Path "./cmake/build"
    cmake ..
    cmake --build .

    # Run the project.
    Write-Host "Testing the application..." -Fore Yellow 
    ./BarcodeReader
}
else {
    Write-Output "Configuring macOS..."

    # Set the library path.
    $dylibPath = Read-Host "Please add the .dylib file path"

    # Copy library files to the destination folder.
    Copy-Item $dylibPath ./cmake/platforms/macos

    # Build the project.
    New-Item -Path "./cmake/build" -ItemType "directory"
    Set-Location -Path "./cmake/build"
    cmake ..
    cmake --build .

    # Run the project.
    ./BarcodeReader
}

Screenshots

Windows

PowerShell windows

Linux

PowerShell Linux

macOS

PowerShell macOS

Source Code

https://github.com/yushulx/powershell-barcode-autobuild