PowerShell Variables Solution
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lecture, I am going to give you the solution to the Variables Challenge that I gave you earlier.
So, If you haven’t completed the challenge just yet, and you don’t want to see the solution go ahead and pause this video, complete the challenge, and then come back because I am going to show you all the code that you need in order to finish this challenge.
I am going to bring up the PowerShell ISE.
Now, this is the code that I used to accomplish the challenge or complete the challenge that I gave you earlier.
So, the first thing I wanted you to do is to store the users First and Last names in two separate variables.
We can see here that we have the $firstName and $lastName variables and I am using the Read-Host to get the user's input to allow them to input whatever they want to enter for their First and Last name.
So, I have “Please enter your first name” and I have -Prompt. Again, below I did not use -Prompt but still accomplishes the same thing. You can probably look at this as being more efficient yet typing fewer commands is probably the best way to do it. I just wanted to show you both so you can be aware you could do both.
The next thing that I specified was that you need to create a new text file under the C:\VariableChallenge folder, and that’s done with the Set-Content command with the -Path argument. So, we have C:\VariableChallenge\ and then the file name.
So, for the file name, I wanted you to specify with today’s date - firstName - LastName .txt. If you remember that today was supposed to be specified in the following format: 2 digit day, 2 digit month, and then the 4 digit year - hours, minutes, and seconds. So, I was able to accomplish that using the Get-Date command and I stored the variable here called $today.
Next, inside of that file I wanted you to use a long date, and I was also able to accomplish this using the Get-Date and -Format with “F”. Now you might be wondering, how was I able to do that? Well, if we run Get-Help Get-Date -Detailed and press Enter.
We are going to get all this information on how to use Get-Date and how to format this Get-Date. But it does not give you complete information, it just tells you how you might want to use it. I was able to construct the format “ddMMyyyy-HHmms” using the following web page:
This is what I was able to use to create these formats. So, I used “F” to get the full date and long time, and this is what I wanted you to include inside of the text file. Next, I was able to use the months, the year, and I can find down through all this information and get whatever I need to do.
If we run the Get-Date -Format “F” command and press Enter I can build the format that I want.
If I add “dd '' and press Enter I can start to build the format. If I add “ddMM” and “ddMMyyyy” and if you look at that web page I’ll find that to get the 4 digit year I need to use “yyyy”.
Then I can use “:” and start specifying the time “ddMMyyyy:HHmms” and that’s the date.
This is how I formatted today’s date. Then, I just used - and I included a $firstName variable that we stored at the beginning of the script and $lastName and .txt.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.