by oising
2. April 2008 21:28
Now this is barely worth blogging, but one of the things I used a lot when I was confined to cmd.exe (yes, "confined" is the word I would use, 4NT aside) is the wonderfully simple copy con filename.txt then type a few lines and end it all with CTRL+Z and enter. So, if you're a PowerShell noob and yearn for this olden-days simplicity like dead parrots pine for the fjords, then this is for you:
- rm function:copy-console -ea 0
- rm alias:cc -ea 0
-
-
- function global:copy-console {
-
- param(
- [string]$Filename = $(Throw "Need output filename."),
- $Encoding = "ASCII"
- )
-
- $out = [io.path]::combine($pwd, $Filename)
-
- $buffer = @()
- $crlf = "`r`n"
-
- do {
- $line = [console]::readline()
- if ($line -eq $null) { break; }
- $buffer += $line
- } while ($TRUE)
-
- $buffer | set-content $out -Encoding $Encoding
- }
- new-alias cc copy-console
-
-
-
-
-
-
-
-
-
-
-
-
I saved this to copy-console.ps1 and aliased it to "cc." Of course, you can do whatever you want - it's probably easier to just put it into a function in your profile. Just place the script above into your profile and remember: CTRL+Z then enter to save.
UPDATE 2008-04-04: Somehow I completely broke this in my attempts to "clean it up" before posting. I've reposted a better version (imho), and implemented encoding support as suggested out in Jason's comment below ;-)