Copy Con For PowerShell?

Line Break

Author: Frank-Peter (77 Articles)

Hello again! This time I share a quick tip.

MS-DOS and cmd.exe shell power users remember for sure how easy it was to write a script (or any other text file) quickly from the command line without an editor. It was possible to copy the CON device (which stands for Console meaning keyboard/input and monitor/output as one device) to a file, for example COPY CON TEST.CMD. Once a COPY CON command has been invoked, it was possible to type whatever you want, even multiple lines were possible. When completed you could save the file and return to the prompt by pressing CTRL-Z (or F6) which would create ^Z (end of file) and then press Return.

In PowerShell there’s a similar approach as well but it doesn’t correspond one-to-one. The trick takes advantage of a single-quoted here-string to create a script:

PS C:\Users\Pit> @'
>> "Hello, $Env:USERNAME!"
>> "There are $((Get-ChildItem).Count) files and folders in this directory: $pwd"
>> '@ > .\test.ps1
>>
PS C:\Users\Pit> type .\test.ps1
"Hello, $Env:USERNAME!"
"There are $((Get-ChildItem).Count) files and folders in this directory: $pwd"
PS C:\Users\Pit>
PS C:\Users\Pit> .\test.ps1
Hello, Pit!
There are 15 files and folders in this directory: C:\Users\Pit
PS C:\Users\Pit>

Actually, Here-strings are used to embed more or less large text blocks inline in scripts. Here-strings start with “@” plus double- or single-quote followed by newline and end with newline, double- or single-quote followed by “@” .

Bookmark and Share
Tags »

Author:Frank-Peter
Date: Wednesday, 27. January 2010 23:30
Trackback: Trackback-URL Category: General, PowerShell

Feed for the post RSS 2.0 Comment this post

1 Comment

  1. 1

    [...] Copy con for PowerShell [...]

Submit comment

CAPTCHA Image CAPTCHA Audio
Refresh Image