Scripting Wife Uses PowerShell to Create Files Automatically


Summary: The Scripting Wife learns how to use Windows PowerShell to automatically create files with the date stored in them.

The room seems quiet now. I take a deep breath, let it out slowly, and begin to relax. My name is Ed Wilson. I am the Microsoft Scripting Guy. I have just completed the second in a series of five Live Meetings during what I call Script Week. Designed as an introduction to Windows PowerShell, these Live Meetings provide a foundation for people wishing to compete in the 2011 Scripting Games. To do this the week before the 2011 Scripting Games kick off, however, was a bit excessive. Answers from the expert commentators are flowing in, status meetings are scheduled, and questions on Twitter, Facebook and via seem to abound.

On top of all that, the Scripting Wife keeps finding my hiding places and asking me to show her stuff about Windows PowerShell. Don’t get me wrong, I love the excitement, and I believe that learning Windows PowerShell will strengthen and enrich one’s life and career as an IT Pro. Consequently, I feel that the Scripting Games are extremely important. In addition, I applaud the Scripting Wife for taking the time to learn Windows PowerShell; it is just that she is becoming obsessive about it. We go out to a nice restaurant for dinner, and she pulls out her laptop and starts asking me questions. We take a trip, and she brings her laptop so she can quiz me. She would have taken her laptop to the movie theatre, I imagine, if I did not put my foot down and said, “No.”

Therefore, I am hiding in one of the guest bedrooms in our house in Charlotte, North Carolina. One of the great things about working from home is that one can take ones laptop to another room to work. One can in fact work from the back porch in nice weather, a guest bedroom during bad weather, or even from the swing on the front porch. Today, it is a guest bedroom on the first floor. I doubt if she will come in here because it is sort of off of the beaten path.

Ring….ring…ring …

“Dude, it is my cell phone. Who is calling me at this time of day,” I thought to myself. I pick up my cell phone and look. It is Scripting Wife. I’m busted.

About that time, the door to the guest bedroom burst open, and there she was. Of all the rooms in all the house, and she walks into mine.

“I found you,” she announced triumphantly. “That cell phone works just like a Script Monkey sonar. All I have to do is call and listen for the ring.”

She knew that no matter where I am working, I need to have my cell phone with me. My Windows 7 Smart Phone is therefore an electronic leash to which she affixes a collar.

“I see,” I answered.

“I have a problem that I believe you can help with,” she said.

“And what is that, my dear?” I asked.

“I need to create five text files. I am going to use these for additional address cards. This time, however, I want to put today’s date on the first line of the file. That way, I know when I added my new address card. I will put the addresses in later. So all I need is to create five files and put the current date on the first line. Will this be hard to do?” she asked.

“I was wondering if you were going to take a breath. But as a matter of fact, it will be extremely easy to do. Do you care what the files are named?” I asked.

“No. I will name the files later. I just do not want to do a lot of clicking around to create the files, and I hate typing dates,” she said.

“OK, the first thing you need to do is to create an array of five numbers. To do this, begin with 1, use the dot dot range operator, and then end your command with 5,” I instructed.

The Scripting Wife thought for a few seconds and hastily typed the following command (<enter> represents the Enter or Return key).


The command and the associated output are shown here.

PS C:\> 1..5






“Well done,” I said.

“Don’t be a smart aleck,” she warned. “It is no big deal.”

“OK. Now what you want to do is to pipe those five numbers across the pipeline to a Foreach-Object cmdlet. Inside the curly brackets of the Foreach-Object cmdlet, you will want to use the New-Item cmdlet to create a new itemtype of file. You will want to use the Get-Date cmdlet with the tostring method to get your date and time stamp as a string. This will be the value to use. You will also want to use an expanding string to create your file name. The file names will be one dot text, two dot text and so on,” I said.

“So basically this task is one long command?” she asked.

“Yes. You can use command aliases if you wish to make the command easier to type,” I said.

The Scripting Wife thought for about a minute, and then began to type. Here is the command that she typed and the associated output. (She decided to use the percentage sign (%), which is an alias for the Foreach-Object cmdlet. She also could have used the ni alias for the New-Item cmdlet and getdate for the Get-Date cmdlet.)

PS C:\> 1..5 | % { New-Item -Path c:\fso -Name “$_.txt” -Value (Get-Date).toString()

-ItemType file}


    Directory: C:\fso


Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-a—         3/29/2011   3:30 PM         20 1.txt

-a—         3/29/2011   3:30 PM         20 2.txt

-a—         3/29/2011   3:30 PM         20 3.txt

-a—         3/29/2011   3:30 PM         20 4.txt

-a—         3/29/2011   3:30 PM         20 5.txt

“Wow, that was pretty cool,” she said.

“Go ahead and open one of the files to make sure it is what you want,” I said.

The Scripting Wife navigated to the c:\fso folder and opened the file 1.txt in Notepad. The file is shown here.

Image of Notepad file

“Cool. Thanks,” she said.

The door did not exactly slam, but it was not closed with delicacy either. I would say that it closed with a sense of finality, but that is not entirely accurate either. I guess I could say she closed it with alacrity. As the sound faded from the room, I realized how quiet things are when she is not around. She is fun to have around, and I miss her when she is gone.

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

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon