Sunday, April 26, 2020

Understanding variables and editing the powershell profile for custom message when logging in in powershell 7 in Debian 10

creating variable

$a=1
$b=2
$c=$a+$b

to see the type of the variable

$a | Get-Member

gm is the alias of Get-Member

so we can do

$a | gm

as gci is the alias of get-child-items

we can do

$items=gci

$items|gm

to get the processes

$processes=get-process

$process | gm

by default the variables are loosely typed,for some reason if we want strict type variables
we need to cast it like this

[int]$dozen=12

if we get the type

$dozen|gm

it will be shown as a integer

Now I can not assign a string inside it for obvious reasons

 $dozen="kshinfo"

so the above line will not execute

to see all the variables

get-variable

in the output we will see the profile.sh file

now from terminal to edit the profile file

code $PROFILE

will open the profile in vscode if it is installed

Now to get a custom message like a greeting
showing the time and the day

we need to write

#Create a greeting that says good morning or good afternoon based on time of the
day
$date=Get-Date

$hour=$date.Hour

$dayofweek=$date.DayOfWeek

if ($hour -lt 12)

{
$greeting="Good Morning,"

}

else
{

    $greeting="Good Evening,"
    }

Write-Host "$greeting I hope you are having a great $dayofweek"





save the file and close it and restart the powershell session

you will see the custom prompt


No comments:

Post a Comment