Instructions | |||||
PowerShell Assignment #1 Objective:
Setup:
Script #1 – Hello World! Description: The classical introductory exercise: Just say "Hello, World!". Purpose: A "Hello, World!" program is traditionally used to introduce novice programmers to a programming language. It is also to make sure that the interpreter is installed correctly, and that the user understands how to use it. Instructions: Complete this assignment to make sure that you know how to start PowerShell and create a new script.
Script #2 – Leap Year Description: Given a year, report if it is a leap year. Purpose: Learn the use of if statements. Instructions: Complete this assignment to make sure that you know how to use if statements in Powershell.
Script #3 – Temp Folder Description: Find the Windows temp folder and get information about the folder. Purpose: Learn how to use Environmental variables. Instructions: Discover the TEMP folder on the local system and basic information about the folder.
|
Script #1 – Hello World!
Following are the Hello World! Power shell script and output
-------------------------------------------------------------
$str1 = "Hello World!"
write-host $str1
--------------------------------------------------------
First line assigns the value Hello World to str1 and the command write-host prints the str1 value to the console
Script #2 – Leap Year
Following is the Power shell script and output to check a given year is leap year or not
---------------------------------------------------------------------------------
$year=Read-Host -Prompt 'Please enter a year'
foreach ($yr in $Year) {
if ($yr / 400 -is [int]) {
Write-Host "$yr is a leap year"
}
elseif ($yr / 100 -is [int]) {
Write-Host "$yr is not a leap year"
}
elseif ($yr / 4 -is [int]) {
Write-Host "$yr is a leap year"
}
else {
Write-Host "$yr is not a leap year"
}
}
-------------------------------------------------------------------------------
The above script checks that the entered year is divisible by 400,100 and 4. If it is divisible by 400 script prints that it is a leap year.Else it check that given year is divisible by 100 , if yes script prints it is not a leap year. Else check by dividing the year by 4 and if yes prints that the year is a leap year. If all the above conditons are false, script will print that it is not a leap year
Script #3 – Temp Folder
Following are the script and output for display information about the TEMP folder
----------------------------------
get-childitem $Env:TEMP
------------------------------------
Get Answers For Free
Most questions answered within 1 hours.