I want to convert JPG to PDF without using software packages or LibreOffice.
I use PowerShell, I found the JPG to PDF with MS print to PDF here: https://community.spiceworks.com/topic/2233896-powershell-convert-jpg-to-pdf-using-pdf-printer
but when running, it shows up window that I have to full name of the file manually. I want to autofill that, but haven't found a way. Hope you guys help me.
i used pass thru but no works. Haven't found any yet
If you are just looking for the input file path, then you can alter that in the code by hard-coding your full path in the Get-childitem section. Change the file path to whatever input file path you want along with the file name, you should be good. I have added a comment for your reference on the same line.
$scandir="C:\IT Tools\Print to PDF Powershell\Test-JPG"$scanlogdir="C:\IT Tools\Print to PDF Powershell\Logs"
$pdftoprint=""
$printername= "Nitro PDF Creator (Pro 9)"
#Change the file name and file path of the jpg file # Cleanup old pdf files - Change AddDays value as required
Get-ChildItem "$scanbdir\*.jpg" | ? {LastWriteTime -LT (Get-Date).AddDays(-15)} | Remove-Item -Confirm:$false
# Create a Log file $scanlogname
$scanlogname = Join-Path -Path $scanlogdir -ChildPath "$(Get-Date -Format 'MM-dd-yyyy').log"
# Get the List of files in the Directory
echo "$(get-date) - Checking for any scanned pdf files in $scandir" | Out-File -Append $scanlogname
(Get-WmiObject -ComputerName localhost -Class Win32_Printer -Filter "Name='Nitro PDF Creator (Pro 9)'").setdefaultprinter()
Get-ChildItem -Path $scandir -filter "*.jpg" | % {
Start-Process -FilePath $_.VersionInfo.FileName –Verb print
#out-printer -InputObject $scandir\$pdftoprint $printername
"$(get-date) - Printing file - $_ on Printer - $printername" | Out-File -Append $scanlogname
}
Related
I have an array of custom PS objects that I want to upload to Sharepoint Online as a .csv file, without saving the array to storage first.
I am using the following code:
$memStream = [System.IO.MemoryStream]([System.Text.Encoding]::UTF8.GetBytes(($devices | ConvertTo-Csv -NoTypeInformation)))
$null = Add-PnPFile -FileName $fileName -Folder $folderPath -Stream $memStream -ErrorAction Stop
When I open the file in Sharepoint or when I download the file to my PC and open it in Excel, I see only the header row. When I do a regular Export-Csv, I see all of the expected rows:
$devices | Export-Csv -Path <path> -NoTypeInformation
How do I get the entire array (as a csv) into the memory stream?
I open a PS in a folder then use
dir -name > asd.xls -recurse.
How can I modify this so it doesn't incude folders in the filenames?
Instead of using -name, try using
(Get-ChildItem -Recurse).Name > asd.xls
and be aware that you won’t get a valid Excel workbook that way. You can get a valid CSV that can be loaded into Excel with
(Get-ChildItem -Recurse) | Select-Object -Property Name | Export-CSV -Path asd.csv -NoTypeInformation
Any help greatly appreciated.
I have a folder that contains 30+ folders which each have a .txt file that I can search for using:
Get-ChildItem -Filter *.txt -Recurse
I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.
The question is twofold,
how to use pipe and powershell commands to read/show all the words in the files.
how to create the csv data that will output both the directory name and the contents of the .txt files.
I can already pipe results to:
c:\desktop\test.csv -Encoding ascii -noTypeInformation
The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.
$csvOut = #()
Get-ChildItem -LiteralPath C:\temp -Filter *.txt -File -Recurse | foreach {
$fileData = #{
"File"=$_.FullName;
"Content"=(Get-Content -LiteralPath $_.FullName -Raw)
}
$csvOut += (New-Object psobject -Property $fileData)
}
$csvOut | Export-Csv -LiteralPath "C:\temp\csvout.csv" -NoTypeInformation
I've created a script that I'm using to clean up some drives at my work. I've been asked to create a log and leave it in the source folder after I move files.
Currently, the process is slow because my script creates a text file of files that meet the parameters I input. Once I have verified it, or edited the text file, I allow the script to read the file and keep doing what it needs to do. I'm creating this original text file with Out-File. Obviously, the path of the files change because I'm moving them from one drive to another. I'd like to log their new path but can't seem to figure out how to do this.
The file the script creates and reads from looks like the following:
C:\This\Is\The\Source\Something.rpt
C:\This\Is\The\Source\Somethingelse.bak
C:\This\Is\The\Source\AnotherFile.jpg
I'm looking to create something that will reflect the new path once the files are moved. In the different ways I've tried I either end up with nothing or just the last file copied, which would tell me Out-File is not appending but overwriting each time it gets a new file path.
And the list will just go on. The following is bit from my script I'm having issue with:
$path = Read-Host "What path should I look at?"
$SourceFolder = $path
$files = Get-ChildItem $path -Recurse
| Where-Object {$_.lastwritetime.Date -eq $targetdate}
|Where-Object {$_.PSIsContainer -eq $false}
| ForEach-Object {$_.fullname}
| Out-File $OutFileCopy
$Target = Read-Host "What is the destination?"
Write-Host "Please view the text file created." -foregroundcolor "yellow" -backgroundcolor "red"
Invoke-Item $OutFileCopy
$CopyContinueAnswer = Read-Host "Would you like to continue? Y or N?"
If ($CopyContinueAnswer -eq 'Y') {
$Readfile = Get-Content $outfilecopy
$ReadFile | Where-Object {$_.PSIsContainer -eq $false}
foreach ($file in $ReadFile) {
$logfile = "$Sourcefolder\log.txt"
out-file $logfile
Write-Host "The old path is $File"
$TargetPath = $File.Replace($SourceFolder, $Target)
if (!(Test-Path $TargetPath)){
Write-Host "This is the new path $TargetPath" -foregroundcolor "Magenta"
Write-Host
Copy-Item -path $file -Destination $TargetPath
Write-output $TargetPath | out-file $logfile
}
Out-File by default will overwrite an existing file. If you do not want this to happen, use Out-File -append. I recommend looking at the Microsoft documentation for Out-File; you can find it by typing Get-Help Out-File at any PowerShell prompt, or clicking on the link.
The file that I want to record the new files paths is being created inside the foreach loop. Everytime it stepped to the next file it would recreate the log.txt file and erase what was there previously. Now that I've taken it out of the loop, I don't have the issue of nothing being recorded or only the last file that went through the loop being recorded.
I'll create a portion of the script that looks to see if the log.txt already exists before it tries to create one.
I have list of share path in a text file. I try to read the files and folders in each path and exporting to csv file using powershell script. I got some csv files with 0 KB.
so i try to test the existance of such network path using Test-Path command. few path shows it is exist but when itry to list out the directories of existing path using Dir \sharepath name i got error like "The specified network name is no longer available" Why??
Sharing code below..
foreach ($dir in (Get-Content $infile)) {
$outfilecsv='jerin-Download'+'.csv'
Get-ChildItem -Path $dir -Filter *.* -Recurse | Select-Object
Name,#{Name="Owner";Expression={(Get-ACL $_.fullname).Owner}},CreationTime,#{Name="FileModifiedDate";Expression={$_.LastWriteTime}},
#{Name="FileAccessedDate";Expression={$_.LastAccessTime}},#{Name="Attributes";Expression=
{$_.Attributes}},#{l='ParentPath';e={Split-Path $_.FullName}},
#{Name="DormantFor(days)";Expression={[int]((Get-Date)-$_.LastWriteTime).TotalDays}},
#{N="FileCategory";E={Get-FileSizeCategory($_)}},
#{Name="Size";Expression={if($_.PSIsContainer -eq $True){(New-Object -com
Scripting.FileSystemObject).GetFolder( $_.FullName).Size} else {$_.Length}}}|
Export-Csv -Path $outfilecsv -Encoding ascii -NoTypeInformation
}
Can anyone suggest
Thanks
Jerin