Powershell Move-Item logging - powershell

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

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.

Get-Content piped with Add-Content

I encountered something weird I do not understand. My scenario:
I have in C:\Functions multiple .ps1 files. I would like to copy the content of the files to one file (AllFunctions.ps1). The file CopyFunctions2AllFunctions.ps1 is the file that execudes my commands.
$path="C:\Functions\*.ps1"
$destination="C:\Functions\AllFunctions.ps1"
Clear-Content -Path C:\Functions\AllFunctions.ps1
Get-Content -Path $path -Exclude "C:\Functions\CopyFunctions2AllFunctions.ps1" | Add-Content -Path $destination
The error message is in german, however, it says AllFunctions.ps1 cannot be accessed, because it is used in another process.
The code works if replace
$path="C:\Functions\*.ps1"
with a specific file name like
$path="C:\Functions\Read-Date.ps1"
-Force didnt help
Also, the code worked until Add-Content -Path $destination. When I executed Get-Content... the terminal didnt show me just what was inside the .ps1 files, but also the content of the terminal, with all the errors I encountered while trying...
Does someone have an idea?
There are 2 things to be fixed in this code, first the new code:
$path="C:\Functions"
$destination="C:\Functions\AllFunctions.ps1"
Clear-Content -Path C:\Functions\AllFunctions.ps1
$functions=Get-ChildItem -Path $path -Exclude CopyFunctions2Profile.ps1 | Get-Content
Add-Content -Path $destination -Value $functions
Issue #1
$path="C:\Functions\*.ps1" doesnt work, PS is also copying the content of the terminal, I dont know why...Therefore, we dont use wildcards in our $path.
Because of that we need to use Get-Childitem in the code as the following:
$functions=Get-ChildItem -Path $path -Exclude CopyFunctions2Profile.ps1 | Get-Content
Issue #2
Working with pipes, PS processes one item and sends it through the pipe, then the second and so on. Due to that, when item 2 is sent to Add-Content, "AllFunctions.ps1" is still being used for item 1.
Therefore, we need to save our Get-Content in a variable ($functions) and then use it in Add-Content.

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

Getting PowerShell to post all information to out file

PowerShell is writing information to file when I delete a registry entry successfully however I also need it to post errors to the file instead of the PS session.
Remove-Item -Path $path -Force -Verbose 4>&1 | Out-File "c:\registryresults.txt"
How would I be best able to achieve this?
4>&1 is the 'Verbose' stream being redirected to the 'output' stream.
you probably want to use *>&1 or just *> "c:\registryresults.txt" on its own without the Out-File

Create PS script to find files

I want to start by saying coding is a bit outside of my skill set but because a certain problem keeps appearing at work, I'm trying to automate a solution.
I use the below script to read an input file for a list of name, search the C:\ for those files, then write the path to an output file if any are found.
foreach($line in Get-Content C:\temp\InPutfile.txt) {
if($line -match $regex){
gci -Path "C:\" -recurse -Filter $line -ErrorAction SilentlyContinue |
Out-File -Append c:\temp\ResultsFindFile.txt
}
}
I would like to make two modifications to this. First, to search all drives connected to the computer not just C:\. Next, be able to delete any found files. I'm using the Remove-Item -confirm command but so far can't make it delete the file it just found.