Windows Tip Sheet

Console Says...

PowerShell version of a CMD trick for getting server names in a text file.

A while ago I showed a CMD tip for creating files on the fly from a command prompt. You can perform this trick with PowerShell several ways. The method that works for you will really depend on what you need to accomplish.

The first technique is great for creating lists, say of server names. Instead of opening Notepad, entering the names and then saving the file, you can try something like this:

PS C:\> "serverA","ServerB","ServerC" | out-file c:\files\list.txt

In PowerShell, you can simply pipe the list of names to a cmdlet to do something. But in this situation, all you want to do is create a simple text file from the prompt. If you want to create something a little more complicated, try this:

PS C:\ > {
>> #test1.ps1
>> get-process | where {$_.workinsgset -ge 500000}
>> write-host "Getting data...please wait"
>> get-wmiobject win32_bios
>> get-wmiobject win32_operatingsystem
>> get-wmiobject win32_computersystem
>> } | out-file c:\test\test1.ps1
>>

This technique works better for longer lines, but be careful. Once you move to the next line you can’t edit a line.

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

Certainly you won’t want to write a major administrative script this way. Still, if you want to create a multi-line file directly from the command line, you can’t beat this approach.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events