0

Okay so I'm working in Powershell and am trying to have "write-host" output to a file. Here is the code I'm working with right now

Function CreateNugetPackage {
    Param(
        [String] $File
    )
    Process {
        $retCode = 0
        write-verbose "Getting version of $file..."
        $version = (Get-Item $File).VersionInfo.FileVersion
        $id = $file.Substring(0, $File.LastIndexof('.'))
        $filepath = Get-ChildItem "$File"
        $OriginalFilename = (Get-Item $File).VersionInfo.OriginalFilename
        write-verbose "$id"
        write-verbose "$filepath"
        $id > test.txt
        write-host $filepath >> test.txt

So the $id generates the name of the file without the extension and what I'm trying to do with the $filepath is have it output the host to the test.txt file. I've tried doing "write-output $filepath >> test.txt" but that didn't work the way I wanted it to. Instead of outputing the host it outputted

Directory: C:\Users\mhopper\Documents\CreateNugetPackage


    Mode                LastWriteTime     Length Name                              
    ----                -------------     ------ ----                              
    -ar--        10/22/2014   9:17 AM    5534208 AjaxControlToolkit.dll   

instead of just "C:\Users\mhopper\Documents\CreateNugetPackage\AjaxControlToolkit.dll"

Any help would be appreciated thank you!

2
  • 2
    Try $filepath.FullName as well. Get-ChildItem returns FileInfo (or DirectoryInfo) objects which PowerShell formats in a special way. Commented Dec 11, 2014 at 16:54
  • Yes! Thank you! That worked exactly like I wanted it to, thank you very much! Commented Dec 11, 2014 at 17:03

1 Answer 1

2

You can't redirect output from Write-Host (it doesn't populate the output stream and cannot be piped).

Sign up to request clarification or add additional context in comments.

2 Comments

I tried doing write-output instead of write host and it generates Directory: C:\Users\mhopper\Documents\CreateNugetPackage Mode LastWriteTime Length Name ---- ------------- ------ ---- -ar-- 10/22/2014 9:17 AM 5534208 AjaxControlToolkit.dll instead of "C:\Users\mhopper\Documents\CreateNugetPackage\AjaxControlToolkit.dll"
@Macauley Look at Mike Z's comment on your post. You need to use $filepath.FullName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.