I have a PowerShell script that looks for .doc files on a folder and subfolders, removes Read Only atribute, converts them to .docx and then moves the .doc file to Recycle Bin. The issue that I have is if the document is password protected it does not get converted to .docx hence it will get deleted. I need to exclude .doc Password Protected files from getting removed. How do I do that? Also i have the "Recycle" module installed. Here is the script:
$invocation = (Get-Variable MyInvocation).Value
$curr_path = Split-Path -parent $MyInvocation.MyCommand.Path
$doc_app = New-Object -ComObject Word.Application
Get-ChildItem -Path $curr_path -Recurse -Include *.doc | ForEach-Object {
$_.IsReadOnly=$false
Write-Host "Working on->" $_.FullName "..."
$document = $doc_app.Documents.OpenNoRepairDialog($_.FullName, $false, $false, $false, "ignorepass") $curr_path with $_.DirectoryName
$docx_filename = "$($_.DirectoryName)$($_.BaseName).docx"
$doc_app.DisplayAlerts = "wdAlertsNone"
$Format= [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatXMLDocument
$document.SaveAs($docx_filename, $Format)
$document.Close()
}
$doc_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc_app)
Get-ChildItem * -Include *.doc -Recurse | Remove-ItemSafely
I guess I have to use the -Exclude cmdled in the last line but i dont know the value for password protected file.