MalcomTucker
MalcomTucker

Reputation: 7477

How can I save a stream to disk in Powershell?

How can I save a stream to disk in Powershell? Any examples appreciated.

(For info, the stream is the return value from a WCF webservice call)

Upvotes: 1

Views: 8105

Answers (1)

vonPryz
vonPryz

Reputation: 24071

Usually, it is useful to Google how to do the thing with C#. Conversion from C# to Powershell is IMHO more easy than from VB.Net, as PowerShell's syntax is a bit similar. From any C# examples, one can easy find out what .Net classes are used and how. Armed with that knowledge, transforming the techniques into Powershell is quite straightforward.

First off, get the data to a variable. Create a FileStream that points to a file. Create a StreamWriter that usest the file stream and write data into it. Like so,

$webPage = [Net.WebClient]::DownloadString("http://www.google.com") # Sample data
$fs = new-object IO.FileStream("c:\temp\out.txt", [IO.FileMode]::Append) # New FileStream to some file
$sw = new-object IO.StreamWriter $fs # A streamwriter will write to the file via filestream above
$sw.WriteLine($webPage) # Write stuff to the file via streamwriter
$sw.Close() # Close writer
$fs.Close() # Close stream

Upvotes: 3

Related Questions