How to search a word in a docx file with powershell? - powershell

I have to examine all of .docx files in a folder and i have to display the name of files which is contain that word I added as param. How can I do it in powershell?

try someting like this:
#Instance of word
$Word=NEW-Object –comobject Word.Application
$Word.visible = $False
#take list of .docx
Get-ChildItem "c:\temp" -file -Filter "*.docx" | %{
$Filename=$_.FullName
#open file and take content of word file
$Document=$Word.documents.open($Filename, $false, $true)
$range = $document.content
#if content have your word, print path of word file
If($range.Text -like "*tot*"){
$Filename
}
$word.Documents.Close($false)
}

Related

Open multiple HTML files and save as XLSX using PowerShell

How do I open multiple HTML files (tabular format) and save them as Excel XLSX format in Windows PowerShell ISE? Directly renaming file extension removes all the formatting. It was working with a single file. I need help with the looping part?
$FolderPath = 'C:\Users\abcd\Desktop\New folder'
$FilePaths = get-childitem $FolderPath -recurse | where {$_.extension -eq ".html"}
foreach($FilePath in $FilePaths)
{
$Workbook = $Excel.Workbooks.Open($FilePath)
$Excel.Visible = $true
$Excel.DisplayAlerts = $False
$OutFile = 'C:\Users\abcd\Desktop\New folder\...xlsx' #Need same file names
$xlSLSXType = 51
$workBook.SaveAs("$OutFile",$xlSLSXType)
}

How to save filename and file size in subfolder text file?

Sorry i am newbie. i only know how to copy/move/delete files using powershell. it will be really helpful if someone can help me in this matter and please excuse my english.
I have multiple sub folders, lets say
C:\test\1
C:\test\2
C:\test\3
inside the folder there is multiple image files. i want to list the sub folder's file name and size and save text files inside the subfolders.
C:\test\1\filelist.txt
C:\test\2\filelist.txt
C:\test\3\filelist.txt
filelist.txt will have something like this
Image1.png 30kb
Image2.png 4MB
Here is a sample script i found from online but i need it to save text file in subfolders based on subfolder file names.
$Folder = 'C:\pic'
$Output = 'C:\output.txt'
$Files = Get-ChildItem -Path $Folder -Filter *.png -File
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($Folder)
foreach( $File in $Files ) {
$objFile = $objFolder.ParseName($File)
$Name = $objFolder.GetDetailsOf($objFile, 0)
$Size = $objFolder.GetDetailsOf($objFile, 1)
$Length = $objFolder.GetDetailsOf($objFile, 27)
$Tab = [char]9
"$Name$Tab$Size$Tab$Length" | Out-File -Append -FilePath $Output
}
```
If you want to use a built application for this, I recommend:
https://www.karenware.com/powertools/karens-directory-printer

Bulking powerpoint files to pdf in same tree of directories

I found this script (https://gist.github.com/mp4096/1a2279ec7b3dfec659f58e378ddd9aee) which is bulking powerpoints to PDF's and are saving them where you run the script.
However, what if one want to save them into the same directories they are found in but swap the parent path to 'PDF' over 'Powerpoint'?
Suppose the tree of dirs looks something like this:
/Parent_dir/Powerpoint/A_1/B/p1.pptx
/Parent_dir/Powerpoint/A/p1.pptx
And then I want to save them into same tree but with folder “PDF” instead (all the directories already exists but are for now empty):
/Parent_dir/PDF/A_1/B/p1.pdf
/Parent_dir/PDF/A/p1.pdf
I tried playing around with the curr_path but I have to create the curr_path inside the Get-ChildItem loop and Im not sure how to.
# Batch convert all .ppt/.pptx files encountered in folder and all its subfolders
# The produced PDF files are stored in the invocation folder
#
# Adapted from http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf
# Thanks to MFT, takabanana, ComFreek
#
# If PowerShell exits with an error, check if unsigned scripts are allowed in your system.
# You can allow them by calling PowerShell as an Administrator and typing
# ```
# Set-ExecutionPolicy Unrestricted
# ```
# Get invocation path
$curr_path = Split-Path -parent $MyInvocation.MyCommand.Path
# Create a PowerPoint object
$ppt_app = New-Object -ComObject PowerPoint.Application
# Get all objects of type .ppt? in $curr_path and its subfolders
Get-ChildItem -Path $curr_path -Recurse -Filter *.ppt? | ForEach-Object {
Write-Host "Processing" $_.FullName "..."
# Open it in PowerPoint
$document = $ppt_app.Presentations.Open($_.FullName)
# Create a name for the PDF document; they are stored in the invocation folder!
# If you want them to be created locally in the folders containing the source PowerPoint file, replace $curr_path with $_.DirectoryName
$pdf_filename = "$($curr_path)\$($_.BaseName).pdf"
# Save as PDF -- 17 is the literal value of `wdFormatPDF`
$opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
$document.SaveAs($pdf_filename, $opt)
# Close PowerPoint file
$document.Close()
}
# Exit and release the PowerPoint object
$ppt_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt_app)
There are of course several ways to handle your use case. The below is just one example.
$curr_path = Split-Path -parent $MyInvocation.MyCommand.Path
$ValidatePath = If (-Not (Test-Path -Path $curr_path))
{(New-Item -Path $curr_path -ItemType Directory).FullName}
Else {$curr_path}
$ppt_app = New-Object -ComObject PowerPoint.Application
Get-ChildItem -Path $ValidatePath -Recurse -Filter '*.ppt?' |
ForEach-Object {
Write-Host "Processing $($PSItem.FullName) '...'"
$document = $ppt_app.Presentations.Open($PSItem.FullName)
$pdf_filename = "$($curr_path)\$($PSItem.BaseName).pdf"
$opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
$document.SaveAs($pdf_filename, $opt)
$document.Close()
}
$ppt_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt_app)

how can i convert a RTF document to docx

I have found something similar on here but when I try running this I get errors.
I was therfore wondering if it would be possible to make a Powershell script that can take .RTF documents and convert them all to .docx documents?
Use this to convert rtf to docx:
Function Convert-Dir($path){
$Files=Get-ChildItem "$($path)\*.docx" -Recurse
$Word=New-Object –ComObject WORD.APPLICATION
foreach ($File in $Files) {
# open a Word document, filename from the directory
$Doc=$Word.Documents.Open($File.fullname)
# Swap out DOCX with PDF in the Filename
$Name=($Doc.Fullname).replace("docx","doc")
if (Test-Path $Name){
} else {
# Save this File as a PDF in Word 2010/2013
Write-Host $Name
$Doc.saveas([ref] $Name, [ref] 0)
$Doc.close()
}
}
$Files=Get-ChildItem "$($path)\*.rtf" -Recurse
$Word=New-Object –ComObject WORD.APPLICATION
foreach ($File in $Files) {
# open a Word document, filename from the directory
$Doc=$Word.Documents.Open($File.fullname)
# Swap out DOCX with PDF in the Filename
$Name=($Doc.Fullname).replace("rtf","doc")
if (Test-Path $Name){
} else {
# Save this File as a PDF in Word 2010/2013
Write-Host $Name
$Doc.saveas([ref] $Name, [ref] 0)
$Doc.close()
}
}
}
Convert-Dir "RtfFilePath";
Code from and attribution: https://gist.github.com/rensatsu/0a66a65c3a508ecfd491#file-rtfdocxtodoc-ps1

how to replace the date across multiple word documents using powershell

Objective: to find all word document files in a specific folder and then find the date within these doc files and change the date before converting all of the files to pdf files.
(Background:- I have like 100 files with dates inside of the files that all need to be uniform. I then need to change all of the files to pdf).
Here's what i have so far:
$scriptHome= Split-Path -parent $Myinvocation.Mycommand.Definition
$word_app = New-Object -ComObject Word.Application
# This filter will find .doc as well .docx documents
Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {
$document = $word_app.Documents.Open($_.FullName)
$pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
}
$word_app.Quit()