Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

default for out-file -encoding ascii

1,394 views
Skip to first unread message

Frank

unread,
Nov 20, 2007, 10:01:02 PM11/20/07
to
Hi,

I use PowerShell to write batch files for execution. As a result, I need to
make sure they are ascii files or they will not execute. I use the -encoding
ascii all the time, is it possible to make this a default, ie:

write-output "string" | out-file -encoding ascii -append test.bat


Jeff

unread,
Nov 20, 2007, 10:35:00 PM11/20/07
to

There really isn't a way to change the way the Cmdlet works, but if
this is something you do a lot, a function might be a good way to go:

function Append-Batch( [string] $filename, [string] $command )
{
process
{
$_ | Out-File -FilePath $filename -Append -Encoding Ascii
}

end
{
$command | Out-File -FilePath $filename -Append -Encoding
Ascii
}
}

PSH$ Append-Batch test.bat "@echo off"
PSH$ 'if not "%1"=="" echo %1' | Append-Batch test.bat
PSH$ Get-Content test.bat
@echo off
if not "%1"=="" echo %1

PSH$ .\test.bat howdy
howdy

Jeff

Marco Shaw [MVP]

unread,
Nov 22, 2007, 1:28:12 PM11/22/07
to

Well if you're willing to run the v2 CTP, this might be possible. I've
not been able to get out-file working, but I think it is just a matter
of time.

You would create a scriptcmdlet, but you'd also have to likely redefine
all of the parameters you might ever use.

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com

Kiron

unread,
Nov 22, 2007, 2:10:58 PM11/22/07
to
Try this:

Cmdlet Out-AsciiFile
{
param(
[Mandatory][Position(0)][string]$FilePath,
[switch]$Append,
[int]$Width = 80,
[ValueFromPipeline][PSObject]$InputObject,
[switch]$Force,
[switch]$NoClobber
)
$CommandLineParameters['Encoding'] = 'ASCII'
Out-File @CommandLineParameters
}

--
Kiron

Marco Shaw [MVP]

unread,
Nov 22, 2007, 3:29:25 PM11/22/07
to

Thanks!

PS Kiron: Email me marcoDOTshawATgmailDOTcom

Kiron

unread,
Nov 28, 2007, 9:57:41 PM11/28/07
to
Cmdlet Out-AsciiFile
{
param(
[Mandatory][Position(0)][string]$FilePath,
[switch]$Append,
[int]$Width = 80,
[ValueFromPipeline][PSObject]$InputObject,
[switch]$Force,
[switch]$NoClobber
)
begin
{
$CommandLineParameters['Encoding'] = 'ASCII'
$wrappedCmdlet = get-command -type cmdlet Out-File
$sb = {&$wrappedCmdlet @CommandLineParameters}
$sp = $sb.GetSteppablePipeline()
$sp.Begin($cmdlet)
}
process
{
$sp.Process($_)
}
end
{
$sp.End()
}
}

--
Kiron
0 new messages