# 📌 AlwaysOnTop Powershell Module

By
,
Powershell
,
Modules
Published 2022-10-29

Link To Download AlwaysOnTop Module
https://www.powershellgallery.com/packages/AlwaysOnTop/1.0.0

Link To Original Site
https://www.labnol.org/software/tutorials/keep-window-always-on-top/5213/

Time to cook up a new module to tackle a bigger probblem
Time to cook up a new module to tackle a bigger probblem

# Quick Catch-up On Life

Yep I had a mini-vaction of not writing so many blogs as often. Mainly due to being 100% focused on my new job and wanting to spend the time in the evening thinking about all the scripts and things I will be writing and stuff I need to do. On-top of having four daughters and an amazing wife, life keeps me pretty busy outside of work too.

I did want to write a blog last weekend, but think I was still in shock that Charles Olivera lost his fight in the main UFC event for last weekend. Like I think I was feeling down in the dumps for him. I was sure his BJJ was going to beat the Russian Sambo, but it wasn't to be his night, so fair-play to Islam for now being the title holder.

Then I got my own dilemma going on with my laptop, a mate who comes to visit on a Sunday for a cup of tea and a catch-up brought this dog along that he is dog-sitting, and it was a little dog. But damn this dog ran across my already decrepid laptop several times! Then the next day my laptop didn't even want to turn on, after resetting the memory and various other things, my laptop finally booted, wahoo! However I then only seemed to have the left-hand-side of my keyboard keys working. Doh! I mean yeah I can use on-screen keyboard to get over it, but not to write a whole blog, I need a fully functional keyboard.

Had to get my car to the garage as well, as not been driving quite right, plus I got an MOT coming up soon. Then find out I need to replace my front-tyres so got that done today, but yeah just one thing after another. Plus being half-term and trying to entertain the kids after I finish work, letting them stay up a bit later to watch movies, it doesn't leave a lot of time to write some blogs.

Went to play pool last night and like I enjoy English pool and the club I go to it is a great atmosphere and the same price to play for a game as what it was back in the day 50p. They recently had all the tables resurfaced, so really is amazing to only pay 50p a game. Makes a very cheap evenings entertainment. I managed to win a few games but lost over-all, I do always feel the main-score doesn't reflect just how close all the games were. But yeah fair-play to my mate he always seems to get the upper-hand.

I am still loving this job I have been working in since late September. I know I have not been there that long but there is just so many great things about this company I work for does for employees that no other companies I have worked for in the past have done for employees. Like it is just an amazing feeling to wake up each morning, knowing I will be doing scripting realted tasks for the majority of my day. Obviously for some-people this might be their nightmare, but for me it is just a dream job so it is an amazing feeling to have landed a position that wants me to automate tasks, and contribute ideas to the team. I have also got to use a few new modules I have been wanting to use for quite sometime but just not got round to it yet. So it was awesome to use ImportExcel and also Selenium which again I have wanted to try for a long-time but just not got round to it. So was awesome to use these modules recently to complete various tasks I had been asked to complete.

Great to use these new to me modules just like reading new comics
Great to use these new to me modules just like reading new comics

# Back To The Problem 💬

So someone I know tweeted their frustration, with a GIF to say how annoying it is that when you are typing something and a pop-up window then suddenly steals the focus of the window and all your typing is lost. Yep this same thing has happened to me a number of times and yes it is irritating. Although these days I can touch-type so can keep a constant eye on the screen, but yeah it is still frustrating.

Anyways I jokingly replied to his tweet saying maybe I should make a module for that? Although I had jokingly suggested it, this suddenly drove me on a mission to find something, or some-way of easily making a given window of your choice that you currently have open, and make that the main focused window. I mean I couldn't find something obvious on the Powershell gallery already out-there (although I was later informed that there was a module out there that did include this functionality) and with that I used the mighty duckduckgo to find me the answer to this tweet problem.

Link To Actual Application
https://always-on-top.en.softonic.com/

# So Why Did You Not Use Crescendo

Just because you can do crescendo doesn't mean you should do crescendo. What am I talking about? Well for starters to get the main benefit from making a CLI into a cresendo module is having that CLI given output in JSON or something similar so you can convert that into Powershell objects. Plus this application is just an excutable, it does not have a CLI to work with, or parameters you can pass it.

So as this excutable just runs as a background process therer was no need in my mind to use crescendo to get this job done.

# Read The Manual 👓

So this was enough information to get my brain ticking, and in my mind it was exactly as I read it on-screen. I needed to double click the application executable to start that as a process, select my window I want to have focus and then press the given hotkeys *Ctrl+SPACEBAR and bish-bash-bosh it is done.

I opened the good-old-faithful Powershell_ISE to get this task done. After about 15 minutes I figured this was the answer to the problem

<#
.Synopsis
   Keeps a particular window in main focus and prevents other windows from stealing the focus
.DESCRIPTION
   Saw someone I know tweet a real dilemma. That they were so pi$$ed off about other windows taking focus whilst they had been typing, then all that typing had been lost. No more my friends. Eliminate real-world problems with this module. Keeping a single window in main focus all the time until you press CTRL+SPACEBAR to un-focus the window.
.EXAMPLE
   Invoke-AlwaysOnTop -ProcessID 600
.EXAMPLE
   600 | Invoke-AlwaysOnTop
.EXAMPLE
   Focus 600
#>
function Invoke-AlwaysOnTop
{
    [CmdletBinding()]
    [Alias('Focus')]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   Position=0)]
        [ValidateScript({if(Get-Process -id $ProcessID){$true}else{throw "$ProcessID is not a valid running PID please try again"}})]
        [int]$ProcessID
    )

    Begin
    {
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
    Start-Process -FilePath "$($PWD.Path)\always-on-top.exe"
    Write-Host -ForegroundColor Green "always-on-top process has been started"
    }
    Process
    {
    [Microsoft.VisualBasic.Interaction]::AppActivate($ProcessID)
    Start-Sleep -Milliseconds 666
    [System.Windows.Forms.SendKeys]::SendWait("^ ")
    }
    End
    {
    Write-Host -ForegroundColor Green "Press CTRL+SPACE on the always on top window to un-focus it. Thank you"
    }
}

# Thanks For Reading 👍

Hopefully this module will make you think about what you can do with reading someone else's tweet dilemmas. As always I like to try and keep things interesting, and to try and make something unique and useful as the end module product.

Never Give Up Anything Can Be Done
Never Give Up Anything Can Be Done