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!

Wednesday, September 10, 2014

#26 : How to send SMTP Mail with Powershell ?

Many of your scripts require to send mail. There are several ways to send mail, but I am introducing you with SMTP mail sending script here. The code will accept below values which either you can hard-code inside the script or you can make a config file or ini file to get the settings. Below is simple piece of code for the same:



How to send mail to multiple recipients?

This can be done by modifying NOTIFY_ID variable with below -

$NOTIFY_ID=som@foo.com,john@foo.com


Make sure you are using a comma(,) to separate two Mail-Ids.

How to Send HTML Formatted mail using Powershell ?

This is another good option which can be done easily with Powershell. You need to just add one more line inside the code:



Easy and Simple. Enjoy Scripting!!

Thursday, September 4, 2014

#25 : Create a ShortCut with Powershell

Creating a Shortcut might be a difficult task in any scripting language in Windows. Not sure how difficult or simple is there in Unix.

Let's take a look on the simple few lines which accomplish this task -

clear
$wsc = New-Object -ComObject ("Wscript.Shell")$sc=Join-Path -Path "d:\test" -childpath "Notepad.lnk"
$slink=$wsc.CreateShortcut($sc)$slink.TargetPath = "C:\windows\notepad.exe"
$slink.WindowStyle = 0
$slink.Hotkey = "CTRL+SHIFT+F"
$slink.IconLocation = "notepad.exe, 0"
$slink.WorkingDirectory = "d:\Test"
$slink.Save() 


Since this uses Wscript.Shell which is VBScript shell, there are some Windows version where you do not see desired output. Please let me know..

Enjoy!

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