Printing with Powershell and files in folders - powershell

I have a script which does some onsite printing. It doesnt work too well at the moment as the below runs for various file types which are sent to a folder to print, but the problem is it will only print 1 document at a time.
Start-Process –FilePath “c:\tests\*.docx” –Verb Print
I had the idea of doing this to get around it:
get-ChildItem "C:\Tests\*.docx" | `
foreach-object {
start-process -verb Print
}
This doesnt seem to work though. So then i tried this:
get-childitem "C:\Tests\*.xlsx" | `
foreach-object {
Start-Process -Filepath "C:\Program Files\Microsoft Office\Office14\EXCEL.exe" –Verb Print }
Also no luck,
Returns this error:
Start-Process : This command cannot be run due to the error: No application is associated with the specified file for this operation.
I think i am maybe not visualing the process here. Any ideas at all anyone on how to achieve printing of every file in a folder via powershell?
Windows 7 64 bit and $PSVersion = 5.0
Thanks in advance

You are very close, start-process needs the full path and name of the file:
Get-ChildItem "c:\tests\*.docx" | ForEach-Object {start-process $_.FullName –Verb Print}
Using a foreach loop should help you too:
$files = Get-ChildItem "c:\tests\*.docx"
foreach ($file in $files){
start-process -FilePath $file.fullName -Verb Print
}

Related

Find and execute a file with powershell

I have to find and then execute a .exe file from a script deployed by our asset management software. Currently it looks like this:
Set-Location $PSScriptRoot
$proc = (Start-Process -FilePath "C:\Program Files (x86)\software\software name\Uninstall.exe" -ArgumentList "/S /qn" -Wait -PassThru)
$proc.WaitForExit()
$ExitCode = $proc.ExitCode
Exit($ExitCode)
As far as I understand the location for the location for the file is set and some users do not have it there hence why it fails.
So I understand that you can search for a program with
Get-ChildItem C:\Program Files (x86)\software\
And execute with Start-process -Filepath
But do I simply combine that with a | or is there an easier way/will it even work.
As commenter suggested, you can use Test-Path to test if a path exists:
$uninstallPath = Join-Path ${env:ProgramFiles(x86)} 'software\software name\Uninstall.exe'
if( Test-Path $uninstallPath ) {
$proc = Start-Process -FilePath $uninstallPath -ArgumentList '/S /qn' -Wait -PassThru
$proc.WaitForExit()
$ExitCode = $proc.ExitCode
Exit $ExitCode
}
I've also made the code more robust by avoiding the hardcoded "Program Files (x86)" directory, using an environment variable. Because of the parentheses in the name of the env var, it must be enclosed in curly braces.
For added robustness, you may read the path of the uninstall program from the registry, as detailed by this Q&A. If you are lucky, the program even stores a QuietUninstallString in the registry, which gives you the full command line for silent uninstall.

Print PDFs to specific printers based on filename

I would just like to preface this by saying I am brand new to Powershell and have been trying to learn by picking things up here and there. I'm currently trying to automate a process within my company using strictly powershell and Adobe reader.
Our company currently is manually printing individual sets of records and a separate cover page, binding them, and sending them off. An idea to automate this process was to fill a folder with a zipped set of .pdfs for the day. This zip file would then be extracted and it's contents moved to another folder. PDFs with the normal set of records listed as "WO-xxxxxx Set" and the cover page as "WO-xxxxxx Cover". All I would need to do is create a simple script that prints these out in order, so that "WO-000001 Cover" is on top of "WO-000001 Set" and then print the next set in the order.
The complication I've run into is that Start-Process -FilePath $File.Fullname -Verb Print only allows me to target a default printer. Our Covers will need to be printed on thicker paper, and as such I thought the best course of action would be to create two printers on the network with the required printer settings. If I could have the script swap between the two printers based on file name then it would solve my issue.
This script is sending the documents to the printer in order but not actually swapping the default printer. I'm sure this is something I've done wrong in my IfElse cmdlet and would appreciate an experts eye in this.
Function UnZipEverything($src, $dest)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
$zps = Get-ChildItem $src -Filter *.zip
foreach ($zp IN $zps)
{
$all = $src + $zp
[System.IO.Compression.ZipFile]::ExtractToDirectory($all, $dest)
}
}
UnZipEverything -src "C:\Users\admin\Desktop\Zip Test\" -dest'C:\Users\admin\Desktop\UnZip Test\'
Remove-Item "C:\Users\admin\Desktop\Zip Test\*.zip"
$files = Get-ChildItem “C:\Users\admin\Desktop\UnZip Test\*.*” -Recurse
ForEach ($file in $files){
If ($files -eq '*Cover*') {
(New-Object -ComObject WScript.Network).SetDefaultPrinter('Test')
Start-Process -FilePath $File.FullName -Verb Print -PassThru | %{ sleep 10;$_ } | kill
(New-Object -ComObject WScript.Network).SetDefaultPrinter('\\RFC-Print01\Collections Tray 6')
}
Else {Start-Process -FilePath $File.FullName -Verb Print -PassThru | %{ sleep 10;$_ } | kill
}
}
Any help would be greatly appreciated.
If you use the verb PrintTo instead of Print, you can specify the printer:
Start-Process -FilePath $File.FullName -Verb PrintTo '\\RFC-Print01\Collections Tray 6' -PassThru
This would allow you to remove the SetDefaultPrinter calls from the script.

PowerShell script to run application (with commands)

I am trying to run an application from C:\Program Files\APP\app.exe with the application built in commands. When I run from the command prompt, I can get the result I wanted. But I would like to use the script which will check other components of servers along with this one to avoid running this command manually. I tried both of the mentioned scripts below & I am not getting any output. It just opens a command prompt window, runs the result, and closes the command prompt,but I would like to get the output in an output file. Any suggestions? Please let me know.
$Output = "C:\Information.txt"
Start-Process -FilePath "C:\Program Files\APP\app.exe" -ArgumentList "query mgmtclass" | Out-File $Output
Additionally, I also tried -
$Output = "C:\Information.txt"
Start-Process -FilePath "C:\Program Files\APP\app.exe" -PipelineVariable "query mgmtclass" | Out-File $Output
I was also thinking that I can write a batch file & get output written in the temp directory & get those output using the command mentioned below -
Get-Content -Path 'C:\Program Files\tivoli\tsm\baclient\dsmerror.log' | select-object -last 15
Can you try using the RedirectStandardOutput parameter instead of | Out-File:
$Output = "C:\Information.txt"
Start-Process -FilePath "C:\Program Files\APP\app.exe" -PipelineVariable "query mgmtclass" -RedirectStandardOutput $Output
Update:
The error you are getting ("Missing an argument") means exactly what it says. I can't see the line of code you ran to get the error, but I can replicate by omitting the value of RedirectStandardOutput. This example uses splatting so the line of code is not so long, and you can see more clearly what RedirectStandardOutput is:
$Output = "C:\Information.txt"
#{
FilePath = "C:\Program Files\APP\app.exe"
PipelineVariable = "query mgmtclass"
RedirectStandardOutput = $Output
} | % { Start-Process #_ }

Run All PowerShell Scripts In A Directory

I have a series of ps1 files in a directory, all of which have different names. I am trying to run them one after another with Start-Process and the -Wait parameter. How does loop through all the files in a directory and run one PowerShell script after another? There are no subfolders, and no files that are not of type ps1.
Here is my start:
$DirectoryList = Get-Content -Path C:\test
foreach ($Directory in $DirectoryList) {
Start-Process -FilePath powershell.exe -ArgumentList ('"{0}\How To Read Me.ps1" -Path "{1}"' -f $PSScriptRoot, $Directory);
}
You could simply use the call operator (&):
Get-ChildItem 'C:\test' | ForEach-Object {
& $_.FullName
}

Closing a file powershell

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/