Powershell

PowerShell in a month of Lunches

One of the things on my to do list is to learn PowerShell to a decent enough level where i am comfortable with it to perform my day to day duties.  Me and scripting have never really got on very well even though i am aware of its usefulness and benefits. Throughout the years i have managed to get away with asking colleagues, using experts-exchange and Google to grab a script which somebody else has already written.

As technology is moving forwards I am seeing the need for scripting and automation more and more especially with PowerShell.  So i have decided to take the plunge and get to learn PowerShell properly.  I have used it over the past few years but never really in anger and usually just a few one-liners when required.

My tool of choice is going to be a book called PowerShell in a month of Lunches by the PowerShell guru Don Jones (http://concentratedtech.com/). The idea behind the book is to split the lessons into 23 lessons that you can do in your lunch hour. Since I generally do not take a lunch break I will be doing these mainly on weekends and in the evening Smile

At the end of each chapter  he sets a few tasks to make sure that you have learnt whatever you have just read. I will be posting the questions and my answers here so that i can re-call them for future reference if required. So the first ones are below:

1) Create a text file that contains the names of the files and folders in C:\Windows. Name the text file MyDir.txt.

[sourcecode language=”text”]
DIR C:\WINDOWS > C:\MYDIR.TXT
[/sourcecode]

2) Display the contents of that text file.

 

[sourcecode language=”powershell”]
GET-CONTENT C:\MYDIR.TXT
[/sourcecode]

3) Rename the file from MyDir.txt to WindowsDir.txt.

[sourcecode language=”powershell”]
RENAME-ITEM C:\MYDIR.TXT C:\WINDOWSDIR.TXT
[/sourcecode]

4) Create a new folder named LabOutput you can either do this in your Documents folder, or in the root of your C: drive.

[sourcecode language=”powershell” htmlscript=”true” wraplines=”true” padlinenumbers=”true”]
NEW-ITEM -ItemType Directory C:\Laboutput

[/sourcecode]

5) Copy WindowsDir.txt into the LabOutput folder.
[sourcecode language=”powershell”]
Copy-Item C:\Windowsdir.txt C:\Laboutput\Windowsdir.txt

[/sourcecode]

6) Delete the original copy of WindowsDir.txt—not the copy that you just made in LabOutput.

[sourcecode language=”powershell”]
Remove-Item C:\Windowsdir.txt

[/sourcecode]

7) Display a list of running processes.

[sourcecode language=”powershell”]
Get-Process

[/sourcecode]

8) Redirect a list of running processes into a file named Procs.txt.

[sourcecode language=”powershell”]
Get-Process >> c:\procs.txt

[/sourcecode]

9) Move Procs.txt into the LabOutput folder if it isn’t in there already.

[sourcecode language=”powershell”]
Move-Item c:\procs.txt c:\Laboutput\

[/sourcecode]

10)  Display the contents of Procs.txt so that only one page displays at a time

[sourcecode language=”powershell”]
Get-Conent c:\laboutput\procs.txt | out-host -paging

[/sourcecode]

 

Leave a Reply

Your email address will not be published.