I am using the Windows PowerShell and I create a file with
cat > test
Once I typed that in, I can type in content for the file.
However, how do I close the file/terminate the content writing for the file test? On Unix based systems, this would be ctrl D, but that does not seem to work for the PowerShell on Windows...
CodePudding user response:
To flesh out Mathias R. Jessen's comment on the question:
In Windows PowerShell, cat is an alias for the loosely equivalent Get-Content cmdlet; however, Get-Content only operates on files.
You can use a here-string for interactively typing content to be saved to a file (press Enter after each line, as usual):
Set-Content test @'
type
your
lines
here
'@ # Type this to finish and save - must be at the *very start* of the line.
If you want to reference variables and expressions in the lines you type, use the expandable (interpolating) here-string variant, which uses " instead of '.
Note:
Interactively, you'll see the second and all subsequent lines prefixed with
>>, which is PowerShell's way of signaling that the command being typed isn't complete yet.Set-Contentis used to save to a file (and is generally preferable for saving text to files); the caveat in Windows PowerShell is that it uses the system's active ANSI code page to create the file (whereas PowerShell (Core) 7 now commendably consistently defaults to (BOM-less) UTF-8); use the-Encodingparameter as needed.PowerShell supports
>(and>>) too, which delegates to theOut-Filecmdlet, and in Windows PowerShell therefore creates "Unicode" (UTF-16LE) files.While syntactically convenient in general, if you were to use
>in the case at hand, you would have to - awkwardly - type> testafter the closing'@delimiter of the here-string, because - unlike in POSIX-compatible shells such asbash->cannot start a command.
