Showing posts with label Powershell Tricks. Show all posts
Showing posts with label Powershell Tricks. Show all posts

Wednesday, September 24, 2014

#27 : Changing Powershell Prompt

When you open Powershell, you get a prompt like - PS C:\Users\Administrator>. The prompt is very good, but after few days of working you feel to have a change. But the good old command 'prompt' does not work in Powershell.

For those who don't know how to change prompt in Command Prompt, you can try below command Prompt -
prompt $$
-or-
prompt $p$g

The same command does not work in Powershell. I tried it very early days and skipped when it did not work. So, the question is - How to change Prompt in Powershell ?

You need to create function named prompt to change the prompt easily. Follow the below steps -
1. Open Command-Prompt of Powershell.
2. Run the below -
function prompt { "Posh >" ; return " " }

The above command you need to run every time you run Powershell. So, this will be a cumbersome approach. So, let's try another approach - Change your profile which is a Powershell script that runs every time.
1. Find the location of $profile using below command -
echo $profile
2. You might get a path like -
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
3. This path does not exists usually. You can goto location Documents and create a folder named WindwosPowershell and file mentioned above. Create it and move to next step.
4. Run the command -
notepad $profile
5. Copy and paste below script in the notepad and save it -


  1. function prompt
  2. {
  3. ${GLOBAL:CNT_OF_PRMT}++
  4. Write-Host ( "Posh (" + $("000000000${GLOBAL:CNT_OF_PRMT} > ").tostring().substring($("000000000${GLOBAL:CNT_OF_PRMT}").length-4 , 4) + ") > ") -foregroundcolor white -nonewline
  5. return " "
  6. }
You might see a command prompt with a line number -
Posh (0064) >
Posh (0065) >

Every time we press enter a value is increased. You can change the foregroundcolor of your choice.
Isn't this glamorous! I like it!

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!

#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...