109k views
2 votes
1. Write a Powershell for loop that runs from 0 to 5 but it will print exactly the following result

0, 2, 4

2.

Write a powershell date format statement to use today's date and then print the time as shown below:

Example: if the statement you are asked to write was run Saturday, October 17, 2020 9:30:01 AM,

it will print the result:

09:30

User Kreozot
by
7.9k points

1 Answer

2 votes

Final answer:

The Powershell for loop to print specified even numbers is achieved using a modulo operation within the loop. For the date format, the Get-Date cmdlet along with the -Format parameter is used to output the current time with only hours and minutes.

Step-by-step explanation:

For the Powershell for loop requirement, you would need to use the modulo operator to achieve the result which prints only even numbers excluding zero, as follows:

for ($i = 0; $i -le 5; $i++) {
if ($i % 2 -eq 0 -and $i -ne 0) {
Write-Output $i
}
}

This loop starts at 0, increments by 1 up to 5, and uses an if statement to check if the number is even and not zero before printing it.

Regarding the Powershell date format statement to print the time in a specific format, use the Get-Date cmdlet combined with the -Format parameter:

Get-Date -Format "HH:mm"

This command will display the current time in a 24-hour format without seconds, just as required.

User Saravana
by
8.0k points