PowerShell Out-File is not working - powershell

The following line of my code simply does not write to file the Out-File command:
Move-Item $item.Path $CaminhoCompleto -Force -WhatIf -Verbose |
Out-File -Filepath $SaidaTXT -Append -NoClobber
On the screen it shows correctly, but the file is empty.

-WhatIf messages are written directly to the console and can't be piped or redirected without running the statement in a different PowerShell process. You can capture the output with Start-Transcript, though.
Start-Transcript -Path $SaidaTXT -Append
Move-Item ...
Stop-Transcript

Related

How to use Powershell output in a pipeline

My purpose is to write some string into a file, and the file path is something like %SystemDrive%\temp.txt. The pipeline looks like
Write-Output "test" | Out-File -FilePath %SystemDrive%\temp.txt
I can get %SystemDrive% by (Get-ChildItem -Path Env:\SystemDrive).Value, but how can I put it into the pipeline?
You could use Join-Path wrapped in parenthesis to set the FilePath for Out-File.
Write-Output "test" | Out-File -FilePath (Join-Path -Path (Get-ChildItem -Path Env:\SystemDrive).Value -ChildPath "\temp.txt")
The path that this yields on my system is:
C:\temp.txt
You can add the -WhatIf switch to the end of the command to see that it works, without actually writing the file.
I'd opt for creating a temp folder on C then output the txt file to that folder.
Get-"something"| Out-File -FilePath C:\temp\temp.txt

Is it possible to "-passthru" to Log file only and not the console when using "Start-Transcript -Path "$LogOutput"

I have a Powershell script where I want the console to output my custom messages only, but I would like to capture what the command is doing in a log only. Is this possible?
Just to make it clearer with an example below. I want to copy all the Sub Directories and Files as they are in their own directory to a new location:
Start-Transcript -Path "$LogOutput" -IncludeInvocationHeader -Append
Get-ChildItem -Path "$Source" | Copy-Item -Destination "$Destination" -Exclude "File*.xml" -Force -Recurse -PassThru
Stop-Transcript
I then use $? to get the success of the result on the console for my custom messages.
The above shows details I find helpful but it outputs the -PassThru on the console which I do not want to display there. If I don't specify it, it outputs nothing in the Transcript log either.
If I try appending to the copy command ...-PassThru | Out-File -FilePath "$LogOutput" -Append (The same Log used for Transcript) it fails as the log file is locked by the Transcript. Is there a way to make this possible, or will I have to append ...-PassThru | Out-File -FilePath "$LogOutput" -Append to all my commands individually?
Many thanks in advance.

Powershell Trasnscript - Output to a file and NOT display in console

I am writing a PowerShell script to gather general information on our servers. I wrote the script so that it outputs to a file called output.txt via PowerShells Start-Transcript cmdlet. Output works fine. However I just want the output in the file and not displayed on the console.
I have been looking and attempting to see if Start-Transcription can suppress the console output but I have not found anything.
This is a very cut down version of the code I am using-
Start-Transcript -path "Path\To\Output\File.txt"
$servers = Get-Content -path "Path\To\Servers\List\file.txt"
foreach ($server in $servers)
{
net view
net use
net session
}
Stop-Transcript
It all outputs to the file correctly but I just would like it to NOT display the script/command results in the console. If that is possible.
Would this work?
$FilePath = 'Path\To\Output\File.txt'
net view | Out-File -FilePath $FilePath
net use | Out-File -FilePath $FilePath -NoClobber -Append
net session | Out-File -FilePath $FilePath -NoClobber -Append
Or bundle it:
Invoke-Command {net view ; net use ; net session} | Out-File -FilePath $FilePath -NoClobber -Append
EDIT based on comment (but written freely from memory on an iphone so maybe minor mistakes):
To run this remotely against a list of servers you first enable Powershell remoting on the servers, many ways to do it and here is one to run in a local powershell session on each server (Runas Admin):
winrm quickconfig
Then, assuming they all have the same login, you can:
$Cred = Get-Credential
$Servers = ’server1.example.com’,’server2.example.com’
Invoke-Command -ComputerNames $Servers -Credential $Cred -ScriptBlock {
Do stuff
Do some other stuff
} | Out-File -FilePath $FilePath -NoClobber -Append
Results are returned as an array so if you want to separate the output per server you could try:
$a = Invoke-Command [...]etc but skip the |Out-File
then do some loop which in essence does this part, giving you the manual way here:
$a[0] | Out-File -FilePath $FilePath1 -NoClobber -Append #result from first computer
$a[1] | Out-File -FilePath $FilePath2 -NoClobber -Append #result from second computer
...
and an example loop:
$a | Foreach-Object {$_ | Out-File -FilePath $Path -Append -NoClobber}
And to read the servernames from a text file, one servername per line:
$Servers = Get-Content -Path ’C:\temp\example.txt’
Invoke-Command -ComputerName $Servers [...]etc

Very Basic query on logging in powershell

I have no programming background and I'm working on doing some very basic tasks with Powershell. Moving files and logging which files moved
I've tried using out-file and write-output none of which are working
Move-Item -Path $sourceDir\*.* -destination $destinationDir -force | Out-File -filepath $Mylogpath -Append
I'm trying to move files from one server to another which I have working however I'd like to log the files I'm moving into a text file and date/time stamp them.
Any pointer gratefully accepted
Move-Item by default does not output anything. To get an output of which files are moved, you can use the -PassThru parameter on Move-Item. This will then tell Move-Item to output the files it is moving:
Move-Item -Path $sourceDir\*.* -destination $destinationDir -force -PassThru | Out-File -filepath $Mylogpath -Append

Powershell Move-Item logging

I would like some assistance getting a powershell script to work correctly. A piece of it is pasted below. Basically what I'm trying to do is to move all files in various subdirectories to another location. I can get the files to move and the folders to create fine, I'm just having a problem with getting the results to a text file. I've tried using out-file, but the txt file is empty. I've also tried start-transcript, but notepad doesn't seem to see line breaks and all the text bleeds together. Is there a better way to go about this? I'm using this basic command for testing.
Start-Transcript c:\log.txt
Move-Item c:\temp\*.* C:\Temp\2014 -Verbose -Force
Stop-Transcript
You may just need to collapse the output file descriptors. According to the about_Redirection page, it should work if you do the following:
Move-Item c:\temp\*.* C:\Temp\2014 -Verbose -Force *>&1 |
Out-File -FilePath $MyLogPath
Or if you want to see the output at the same time
Move-Item c:\temp\*.* C:\Temp\2014 -Verbose -Force *>&1 |
Tee-File -FilePath $MyLogPath