简体   繁体   中英

Have write-host output to a file in Powershell

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!

您不能重定向来自Write-Host的输出(它不会填充输出流并且无法通过管道传输)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM