From Linux Shell to PowerShell: My First Script on Windows

Yes, I know Linux Shell is the best. And this notion has kept me stuck in learning something for Windows, like Command Prompt or PowerShell. But I work on Windows, my company needs me to. However, I am not limiting myself from becoming a pragmatic programmer just because I don’t get to use Linux or Apple command shells. Yes, I have read the book. It’s just awesome.

One thing about Linux Shell that I loved to do was to create Shell Scripts. Let’s do the same in Windows using PowerShell.

Shell Script for Opening a Website

  1. Create a simple TextFile and rename it to filename.ps1
  2. Right-click and select Edit. This will open the file in PowerShell ISE, which is like an IDE for creating PowerShell Scripts.
  3. Write the Script
$chromeBrowserPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
$url = "https://www.abdulbasitbhat.com"

if (Test-Path $chromeBrowserPath){
    Start-Process $chromeBrowserPath $url
}
else{
    Write-Output "Error"
}

Now open PowerShell in the folder where you kept the file. One way to do this is in the file location bar at the top, type “PowerShell” and press Enter.

To enable our custom scripts to run, we need to execute a command

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

RemoteSigned helps us to run local scripts without a digital signature, and -Scope CurrentUser makes this exception possible only for the current user and not system-wide. A digital signature is assigned by a Certification Authority, just like SSL certificates for a website. It ensures that the author is legitimate and the script has not been tampered with. By default, PowerShell blocks all external scripts without a certificate. For our script, we can bypass this with the above command.

For execution simply

./filename
or
./filename.ps1

Shell Script for Hourly Reminders

The script goes as

param(
   [String]$Message =""
)

Add-Type -AssemblyName PresentationFramework

while($true){
   [System.Windows.MessageBox]::Show($Message,"Reminders")
   Start-Sleep -Seconds 3600 
}

Here, the param helps us define a parameter we can pass directly using the command line.

Add-Type is used to add a .NET Framework type to the current PowerShell Session. Using this, we can load a PresentationFramework Assembly, which is needed for GUI rendering in Windows, like MessageBoxes.

In the loop, we use the Show method from the MessageBox class present in System.Window namespace. Now you know why we needed this session to be a .NET framework type.

To run this

.\hourlyReminder.ps1 -Message "1.Drink Water`n2.Stretch a bit"

Now, to get more advanced in this, we can make it run in the background even when the PowerShell window is closed.

powershell -WindowStyle Hidden -File "D:\Powershell Scripts\hourlyReminder.ps1" -Message "1.Drink Water`n2.Stretch a bit"

Now, this can be an issue because if we don’t know how to stop this. But now we know.

Get-Process powershell | Where-Object { $_.MainWindowTitle -eq "" } | Stop-Process

The above command takes all PowerShell processes and checks which one has no title, i.e, means it is invisible / running in the background. Then we stop it. The “|” symbol is called a pipe and executes after the previous statement is executed, while using the result from the previous

Leave a Reply

Your email address will not be published. Required fields are marked *