Summary: A Windows PowerShell Carol in which Ebenezer Script learns how to create and to delete text files using PowerShell.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! Will Ebenezer Script ever see how easy it is to use Windows PowerShell to work with text files?

-- SK

 

Hey, Scripting Guy! AnswerHello SK, Microsoft Scripting Guy Ed Wilson here. It is almost the end of the year and we have decided to devote some posts to the holiday season. We even have guest bloggers from around the world to share some holiday spirit. This week we have Sean Kearney. You can learn more about Sean in Monday’s blog introduction. Be sure to check out Sean’s theme song for this week’s PowerShell Carol posts.

 

When Ebenezer awoke, his head felt less fuzzy but he was still perplexed.

“I must lay off the sugar; a giant 8” floppy indeed!”

He tried to shake off the events and get clarity in his head.

He went downstairs to fetch a cup of tea from and an old “Nutrimatic” he’d picked up on the internet. He sat down and began arguing with it (as was typical) when he noticed a glowing from within the living room.

Half-curious / half out of his mind, he wandered off into the living area. In there he encountered a site he never thought to see again. A fifty foot green-bar printout covered in SET statements and billions of quotes. The world’s biggest VBScript.

“E-B-E-N-E-Z-E-R,” the giant VBScript boomed out, “I AM VBSCRIPT! The Spirit of Scripting Present; touch my code and learn this night!”

Ebenezer looked up. He gazed over the thousands of lines of code. It was actually his old Administration script from years past. He walked up to touch the lines of code, a distant friend from the past.

The giant Script rolled up Ebenezer into its many comments and whisked him away into the air.

Moments later, they found themselves in front of a small abode, stuck in a tiny city in Canada.

“Do you know where we are Ebenezer?”

The Spirit invoked itself through a cscript.exe statement.

Ebenezer blinked, “Why it appears to be Bob Snipit’s place. My assistant.”

“Mr. Script, would you like to explain to this poor old VBScript just WHY your assistant lives in Canada? What a tremendous commute THAT must be!”

“Cheaper rent and an income tax loophole,” mumbled the old mizer as he cursed these all-knowing spirits for revealing what a cheap person he was.

Ebenezer looked in the tiny 100mm x 100mm window.

“What is he doing in there Spirit? He seems oddly happy as poor as he is.”

The spirit invoked. “He is playing a game with his son Tiny Shim. He is coding lines in VBScript while Tiny Shim teaches him how to do it with Windows PowerShell.”

“Well Tiny Shim,” Bob Snipit smiled away, “Mr. Script had me create a new file this morning in VBScript to store his contacts. It was very easy. We just used this code.”

Set ManageFiles = CreateObject("Scripting.FileSystemObject")
Set RemoveFile = ManageFiles.CreateTextFile “C:\TempFolder\NewFile.txt”,True
NewFile.Close

 

Tiny Shim looked on. “Oh, Father! Silly you. You know as well as I, that’s just one line in Windows PowerShell. Remember?”

NEW-ITEM -Path C:\TempFolder -Name Newfile.txt -ItemType File -Force

 

Bob chuckled. “Right you are Tiny Shim! I had nearly forgotten! Then later today we had to remove another file in VBScript using this code.”

Set ManageFiles = CreateObject("Scripting.FileSystemObject")
Set RemoveFile = ManageFiles.DeleteFile “C:\TempFolder\OldFile.txt”

 

Tiny Shim looked up at his Dad giggling. “Father you know just as well as I removing that with Windows PowerShell is only:”

REMOVE-ITEM –Path C:\TempFolder –Name OldFile.txt

 

Bob pretended to be dumbfounded slapping his forehead. “Oh my! But I thought I could do it like this in Windows PowerShell!”

RD C:\Tempfolder\Oldfile.txt

 

The young lad looked at his father as if he had just swallowed a bag of alligators. “You know as well as I do Father, that’s an ALIAS in Windows PowerShell. Aliases allow Windows PowerShell to mimic older commands like in DOS!”

Bob Snipit looked on with pride at his young son. “Such a bright boy!” he tapped him on the head with an old coffee stir stick.

They went back and forth like this for hours. The father and son were teaching each other Windows PowerShell. Tiny Shim loved to code. He loved to sing. He loved to sing and code. Sometimes he even sang songs that were written in code.

“Ebenezer,” the giant VBScript invoked, “see the joy Windows PowerShell brings to Tiny Shim, such wonder and such amazement. He is only eight years old, and yet already a brilliant coder. Someday he will change the world.”

Mr. Script looked on; a child enjoying scripting. Could he have been wrong about Windows PowerShell?

“Take me away Spirit. I suddenly feel something I haven’t felt in a long time. I must be sick.”

“No Ebenezer, it is probably your conscience,” the script muttered deep and low as it whisked Ebenezer back to his abode in Redmond.

 

Will Ebenezer now look at Windows PowerShell as a scripting solution? Will Tiny Shim become the great coder he should be and change the world? Will Mr. Script seriously consider a cost of living adjustment for Bob Snipit to offset his heavy commute to Canada (or at least let him move into the broom closet)?

 

SK, that is all there is to using Windows PowerShell to work with files. Holiday guest blogger week will continue tomorrow when Sean will bring to us another day of A Windows PowerShell Carol.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy