Copy Con For PowerShell?
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 “@” .
Wednesday, 3. February 2010 5:03
[...] Copy con for PowerShell [...]