Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a script that writes output to a log file and also the console. I am running the command Add-WindowsFeatures... I want to take output of this command and pipe it to my script. Is it possible?

share|improve this question
Do you want the output of the log file or the output of the script? – zdan yesterday
I want the output of command add-windowsfeature piping to this script, which writes to the logfile – Naz 11 hours ago

1 Answer

Absolutely. You just need to include the CmdletBinding attribute on your param statement. Then, add an attribute to one of your parameters which details the way the pipeline input binds to the parameter. For instance put this in c:\temp\get-extension.ps1:

[CmdletBinding()]
Param(
[parameter(Mandatory=$true,
            ValueFromPipeline=$true)][System.IO.FileInfo[]]$file
)

process {
  $file.Extension
}

Then, you can do this:

dir -File| C:\temp\get-extension.ps1

updating to address the latest comment: I'm guessing that setting the parameter type to [object[]]$stuff rather than [fileinfo[]] and putting

$stuff | out-file c:\logs\logfile.txt  #or wherever you want

in the process block will get you close.

share|improve this answer
but when I do add-windowsfeatures| c:\....ps1, I just see microsoft.Windows.ServerManager.Commands.FeatureOperationResult in my log file. I am not seeing output from this command – Naz 10 hours ago
Does this work: $stuff.ToString() | out-file c:\logs\logfile.txt – Mike Shepard 10 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.