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
The ‘cd’ command cannot work for changing drives in Command Prompt.
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.csvGet-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
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
- CMake
- Dynamsoft Barcode Reader
- PowerShell
- Git
- Visual Studio 2015
Basic ideas
- Select your operating system.
- Get the source code of barcode reader app.
- Set the absolute path of your barcode SDK.
- Build the project.
- 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 $sourceURLif ($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 Yellowif ($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
Linux
macOS
Source Code
Originally published at www.codepool.biz on March 26, 2018.