I'm using Powershell to automate some tasks, and I've worked out how to edit a .rtf file template and save this back to disk using $content | Set-Output $fileName
This generates a brilliant looking rtf file, complete with bolds, italics, alignment etc.
I'd now like to automatically print this file to the default printer, which is installed with the correct drivers on the machine running the script.
I'm doing this using $content | Out-Printer, but this produces what I presume is the plaintext encoding of the rtf file, rather than the actual formatted document.
Is there any way to print a rtf file with the correct formatting, or am I doomed to plaintext for eternity?
If your .rtf files have a Print option in their context menu, that can be invoked in PowerShell using Start-Process with the -FilePath and -Verb parameters. In its simplest form:
Start-Process -FilePath 'c:\rtfFiles\SomeFile.rtf' -Verb Print
And of course, in real life, the path will most likely be contained in a variable and the Cmdlet inside a loop:
Get-ChildItem *.rtf | ForEach{
Start-Process -FilePath $_.FullName -Verb Print
}
Related
On Linux if I am in a directory and I want to open all .py files with an application (such as atom), I would simply type atom *.py and then all .py files will open with atom. If I type the same thing in Powershell, I receive an error, so I assume the syntax is different on Powershell. How would I accomplish this with Powershell?
Sorry if this is a very beginner question, its my first time using Powershell.
Collect the files you want to open first, then pipe them to the call of your external program:
Get-ChildItem -Path .\* -Include *.py | ForEach-Object {Start-Process -FilePath atom.exe -ArgumentList "`"$($_.FullName)`""}
Adjust the path to your external program if needed and also the the argument list (named arguments instead positional for example). Through the special quoting, this statement is prepared to even handle filenames with spaces.
If this line is to long for your *nix background, you can shrink it to:
gci *.py|%{start atom "`"$($_.FullName)`""}
Try this:
& "Full path of atom.exe" #("*.py")
If atom is set as the default for items with the .py extension you could run gi *.py. Assuming this is not the case i'd right some function as part of a profile script to do this:
$AtomPath = "Path\To\Atom.exe"
function openWithAtom{
Get-ChildItem -Path .\* -Include $args[0] | ForEach-Object {Start-Process -FilePath $AtomPath -ArgumentList "`"$($_.FullName)`""}
}
new-item alias:atom -value openWithAtom
then you would run it with this: atom *.py, or atom filename.py
This tutorial should help with seting up a profile https://www.howtogeek.com/126469/how-to-create-a-powershell-profile/
Also if you haven't already id style your window so it looks more like a terminal.
I am trying to pass a .txt file with arguments to an .exe file via powershell. Currently, this is what I have.
Write-Host "starting upgrade at $(Get-Date -format 'U')"
C:\dev\temp.exe.exe /DIR="C:\TEST" /BTPServerHost="Test" /DBInstance="testDB" /Log=C:\path\to\test\testlog.txt
This is calling a function within an InnoScript file that accepts command line input.
How would I format the .txt file, and how would I be able to pass it into the .exe? Any help would be appreciated! Thanks!
If you are saying, in this text file, there are just these argument line on individual rows and you are saying you've already tried something like the below and were not successful?
You also don't need the Write-Host for the message line, since the default is output to screen. You normal only need Write-Host for colorizing screen text, and a few other formatting cases, depending on what you are doing. All-in-All, Write-Host should be avoided.
"starting upgrade at $(Get-Date -format 'U')"
($ConsoleCommand = Get-Content -Path 'd:\temp\input.txt' -Raw)
# Results - showing the commands in the file before process them
whoami
get-date
'hello world'
Without using the -Wait switch, this will spawn 3 separate PowerShell consoles with the results
ForEach($CmdLine in $ConsoleCommand)
{ Start-Process -FilePath powershell -ArgumentList "-NoExit","-Command &{ $CmdLine }" }
you can of course point to your .exe vs what I am doing here.
Start-Process
By adding the -Raw after specifying the .txt file path it ignores newline characters and returns the entire contents of a file in one string with the newlines preserved. By default, newline characters in a file are used as delimiters to separate the input into an array of strings.
This script takes parameters from a txt file and passes them into an executable and auto-populates the fields in the installation wizard. This is what I'm looking to do, but I don't want to start a new process for each argument in the input txt file.
Write-Host "starting upgrade at $(Get-Date -format 'U')" Get-Content -Path C:\TestInput.txt -Raw | foreach {Start-Process C:\dev\test.exe -ArgumentList $_}
The TestInput.txt file passed in looks like this:
/DIR="C:\TEST"
/ServerHost="Test"
/DBInstance="testDB"
/Log=C:\testlog.txt
I am trying to write a script that opens all pdf files in a folder, prints them, and then closes the files. I have the first 2 parts working, however I can not find a way to close the files after. I've done some google searching but found nothing. I'm new to powershell so if there is a better way to doing this please let me know:
Get-ChildItem -Filter *.pdf |
Foreach-Object {
# File item variable is $_
Write-Host "Printing: $($_.Name)"
#Open the file with a print command.
Start-Process -FilePath $_.FullName -Verb Print
}
Any tips would be appreciated!
I don't have experience with this but was curious about the Start-Process cmdlet with print verb. I Googled that and came up with this that looks to be a good solution to your problem:
Start-Process -FilePath $.FullName -Verb Print -PassThru | %{sleep 10;$_} | kill
Source: http://gregcaporale.wordpress.com/2012/01/18/powershell-to-print-files-automatically/
I search a way to do an automated task with Notepad++ from command line:
Open file
Change encoding to UTF-8
Save file
Is there any way to do it with some plugin or even with other program ?
Why do you want to use Notepad++ for that task? Which OS are you using?
Notepad++ got a Plugin-manager where you can install the Python Script plugin.
http://pw999.wordpress.com/2013/08/19/mass-convert-a-project-to-utf-8-using-notepad/
But if you want to convert files to UTF8 you can do that way easier with PowerShell on Windows or command line on Linux.
For Windows Power-Shell:
$yourfile = "C:\path\to\your\file.txt"
get-content -path $yourfile | out-file $yourfile -encoding utf8
For Linux use (e.g.) iconv:
iconv -f ISO-8859-15 -t UTF-8 source.txt > new-file.txt
Windows Powershell script to change all the files in the current folder (and in all subfolders):
foreach ($file in #(Get-ChildItem *.* -File -Recurse)) {
$content = get-content $file
out-file -filepath $file -inputobject $content -encoding utf8
}
If you want to change only specific files just change the *.* (in the first line).
Note: I tried the pipe (|) approach in Broco's answer and was not working (I got empty output files as Josh commented). I think is because we probably cannot read and write directly from and to the same file (while in my approach I put the content into a memory variable).
I have a bat file that performs some actions and I need to encode a text file with UTF-8 format.
Is there any way to perform this in windows command line??
Thanks in advance.
Only with other programs which may or may not be installed. If you're targetting Windows 7 and higher you could just use PowerShell:
powershell -Command "&{ param($Path); (Get-Content $Path) | Out-File $Path -Encoding UTF8 }" somefile.txt