Very Basic query on logging in powershell - 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

Related

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 Out-File is not working

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

Powershell restore previous version of the files

We got hit by virus, it changed all common file extension to .kmybamf (.txt >> .txt.kmybamf ) and if I delete .kmybamf , the file got damaged.....
So I made a list of all files that got damaged. now I'm trying to overwrite them by previous version. Anyone knows how to do it in Powershell?
I can do it in cmd similar to this
subst X: \localhost\D$\#GMT-2011.09.20-06.00.04_Data
robocopy Z: D:\Folder\ /E /COPYALL
But I want to do it in one shot in Powershell, It has to be a "if .kmybamf found, then restore previous version." and powershell seems like has no such cmdlet for restoring previous version of files or folders.
$fileList = Get-Content -Path "\\localhost\D$\#GMT-2011.09.20-06.00.04_Data"
$destinationFolder = "D:\Folder\"
foreach ($file in $fileList)
{
Copy-Item -Path $file -Destination $destinationFolder -Force
}
This will also work but I find it less readable
Get-Content -Path "\\localhost\D$\#GMT-2011.09.20-06.00.04_Data" | ForEach-Object { Copy-Item -Path $_ -Destination "D:\Folder" -Force }
Get-Content is for reading the text from the files, to read the files from a folder you would have to use Get-Childitem

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

How to move a file to another directory and change the content of it using pipe in one line in Powershell?

Below code does not work. But doing them seperately in two line works.
Move-Item file.txt \same-directory | Set-Content -Path \same-directoy -Value "New content"
The Move-Item cmdlet without switch -PassThru returns nothing, so in that case, nothing gets sent through the pipe to the Set-Content cmdlet.
If you really want to do this as one-liner, use
Move-Item -Path 'D:\file.txt' -Destination 'D:\some-directory' -PassThru | Set-Content -Value "New content"
Since now we're actually piping the moved object, the -Path parameter for the Set-Content needs to be omitted.
Of course, the destination folder D:\some-directory needs to exist