Friday, August 1, 2014

#20 : Moving Cursor to Specific Location

Powershell is full of all possibilities which you might think. I believe there is nothing which can be done by other tools and can not be done in Powershell. This is great about Powershell. So, if you assume that something can not be done - think twice there might be some alternative.

Long back when I was learning Shell Programming, I used tput cup command to move cursor and print something in x,y co-ordinates of screen. This option was not in Batch Programming which I always wanted and it was to much effort consuming to make a Console-Based interactive screen.

Below code can be used to accomplish it:

cls 

function move-the-cursor([int]$x, [int] $y) 
{
 $Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates $x , $y 
  $Host.UI.Write('Hello') 
} 



move-the-cursor 8 8 
echo "Tested by Som : OK" 

When you run the above it moves the cursor to a different location and then prints Hello.
The above sample is enough for you to understand the concept.

Thanks for reading this article!

1 comment:

  1. This also works and is a bit shorter and easier to remember:

    $Host.UI.RawUI.CursorPosition = @{ X = $x; Y = $y }

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...