Write commands (i.e. input not output) to a file - powershell

The Out-File lets us write output to a file. What if we want to write input to a file?
I want a list of all the commands that I wrote during a PowerShell session. The file should look something like this:
dir
cd
ni myFile.txt -t file
cd..
We want to keep track of what commands we use in PowerShell.

Try this:
Get-History |
select -ExpandProperty commandline |
Add-Content commandhistory.txt

You could make use of Start-Transcript and Stop-Transcript
Technet links about each command: start-transcript and stop-transcript
Usage example: Start-Transcript -Path C:\Folder\Transcript.txt

Related

Powershell robocopy logging

How can you log to a text file only new files that have been robocopied from source to destination. I've tried the robocopy /LOG:file command however that logs everything
You can use the Out-File function in powershell in order to output logs to text file instead of outputting them in the shell.
$execution = #some robocopy function to run
$execution| Out-File -LiteralPath "your_path"

Is there a way to "get-clipboard" and use the referenced clipboard info in a command line?

I'm making a script to convert a PDF to txt file.
I'm trying to copy a file name and then use the copied file name in the next line of the script. But using get-clipboard doesn't include that data in the same command line. Is there a way to essentially ctrl+v it in that line using PS?
PS C:\Users\PiRho> #(get-childitem C:\Users\PiRho\Desktop\PDF_Convert -name) [0] | set-clipboard
PS C:\Users\PiRho> cd C:\Users\PiRho\Desktop\PDF_Convert
PS C:\Users\PiRho\Desktop\PDF_Convert> .\pdftotext -table | get-clipboard
I/O Error: Couldn't open file 'get-clipboard'
So this is the old way I was doing it.
PS C:\Users\PiRho> #(get-childitem C:\Users\PiRho\Desktop\PDF_Convert -name)[0] | set-clipboard
PS C:\Users\PiRho> cd C:\Users\PiRho\Desktop\PDF_Convert
PS C:\Users\PiRho\Desktop\PDF_Convert> .\pdftotext -table #Ctrl+V#
The #Ctrl+V# is done using my macro, but it will sometimes use the previous clipboard info.
Effectively I'm looking for a replacement Ctrl+V in powershell that doesn't rely on a macro to put the file name there.
Easiest way is to use Variables so something like:
$File = #(get-childitem C:\Users\PiRho\Desktop\PDF_Convert -name)[0]
cd C:\Users\PiRho\Desktop\PDF_Convert
.\pdftotext -table $File.FullName

Redirect output/logs to file in pipelined commands

I have the below command where I am trying to get output redirected for both commands before and after the pipe.
But this creates csv file with no data.
(some-command) > $log file | export-csv $csvpath >> $logfile
But when I run the command as below, data is returned
(some-command)| export-csv $csvpath >>$logfile
I want a way where I can redirect output for both commands in a single statement.
There are ways to use Tee-Object to pass output to a file and down the pipeline at the same time. You can also use ; to separate statements without needing a new line. Here's an example using both:
(get-date | Tee-Object -FilePath $logfile | Export-Csv $csvpath) ; gc $csvpath >> $logfile
Have a look at Tee-Object which should be pretty close to that what you want

using "CON" as filename

I was copying a huge number of png and txt files using Copy-Item cmdlet, and sadly I discovered that a funny programmer decided to use "CON" as file name to recap connection information.
Given that "con" is a reserved word and Copy-Item returns:
Copy-Item : Cannot process path 'xxx\con.txt' because the target represents a reserved device name.
and given that this name can't be changed and it's used in every folder I need to copy,
Is there a way to copy all these "con.cfg" and "con.txt" files using Powershell?
I googled but I found only advice like "Don't use con!" or "Don't use Powershell to copy these files".
I haven't been able to find a solution for PowerShell yet, but you should be able to rename the files via command prompt using something like this:
ren \\.\<absolute path> <new name>
So for example:
ren \\.\C:\stuff\con.cfg stuff.cfg
You could invoke the command prompt through PowerShell, of course:
cmd /c "ren \\.\C:\stuff\con.cfg stuff.cfg"
And obviously you could use PowerShell variables in there if you wanted
$dir = "C:\stuff"
cmd /c "ren \\.\$dir\con.cfg stuff.cfg"
You could try referring to them using a wildcard: *on ?
Example:
ls | ? {$_.Name -match "*on.cfg"} | del
Regex example:
ls | ? {$_.Name -match "^\won\.cfg"} | del

How to redirect the output of a PowerShell to a file during its execution

I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:
.\MyScript.ps1 > output.txt
How do I redirect the output of a PowerShell script during its execution?
Maybe Start-Transcript would work for you. First stop it if it's already running, then start it, and stop it when done.
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# Do some stuff
Stop-Transcript
You can also have this running while working on stuff and have it saving your command line sessions for later reference.
If you want to completely suppress the error when attempting to stop a transcript that is not transcribing, you could do this:
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue" # or "Stop"
Microsoft has announced on Powershell's Connections web site (2012-02-15 at 4:40 PM) that in version 3.0 they have extended the redirection as a solution to this problem.
In PowerShell 3.0, we've extended output redirection to include the following streams:
Pipeline (1)
Error (2)
Warning (3)
Verbose (4)
Debug (5)
All (*)
We still use the same operators
> Redirect to a file and replace contents
>> Redirect to a file and append to existing content
>&1 Merge with pipeline output
See the "about_Redirection" help article for details and examples.
help about_Redirection
Use:
Write "Stuff to write" | Out-File Outputfile.txt -Append
I take it you can modify MyScript.ps1. Then try to change it like so:
$(
Here is your current script
) *>&1 > output.txt
I just tried this with PowerShell 3. You can use all the redirect options as in Nathan Hartley's answer.
powershell ".\MyScript.ps1" > test.log
If you want a straight redirect of all output to a file, try using *>>:
# You'll receive standard output for the first command, and an error from the second command.
mkdir c:\temp -force *>> c:\my.log ;
mkdir c:\temp *>> c:\my.log ;
Since this is a straight redirect to file, it won't output to the console (often helpful). If you desire the console output, combined all output with *&>1, and then pipe with Tee-Object:
mkdir c:\temp -force *>&1 | Tee-Object -Append -FilePath c:\my.log ;
mkdir c:\temp *>&1 | Tee-Object -Append -FilePath c:\my.log ;
# Shorter aliased version
mkdir c:\temp *>&1 | tee -Append c:\my.log ;
I believe these techniques are supported in PowerShell 3.0 or later; I'm testing on PowerShell 5.0.
One possible solution, if your situation allows it:
Rename MyScript.ps1 to TheRealMyScript.ps1
Create a new MyScript.ps1 that looks like:
.\TheRealMyScript.ps1 > output.txt
You might want to take a look at the cmdlet Tee-Object. You can pipe output to Tee and it will write to the pipeline and also to a file
If you want to do it from the command line and not built into the script itself, use:
.\myscript.ps1 | Out-File c:\output.csv
To embed this in your script, you can do it like this:
Write-Output $server.name | Out-File '(Your Path)\Servers.txt' -Append
That should do the trick.