CategoriesUncategorized

Remove Classification footer All Documents with Powershell !

When you need to edit footers in multiple documents, doing it manually can be time-consuming. If you need to remove a specific footer like “Classification” from all documents, automating the process can save you a lot of time. PowerShell is an ideal tool for this task. In this post, we’ll walk you through how to use PowerShell to delete the “Classification” footer from all your documents.

Requirements

  • Windows operating system
  • PowerShell
  • Microsoft Word installed (PowerShell will use COM objects to manipulate Word documents)

Preparing the PowerShell Script

  1. Setting PowerShell Permissions

First, ensure PowerShell has the necessary permissions to run scripts. Run PowerShell as an administrator and enter the following command:

Set-ExecutionPolicy RemoteSigned





This command allows the execution of signed scripts.

  1. Writing the PowerShell Script

The following PowerShell script will open all Word documents in a specified folder, remove the “Classification” footer, and save the changes:

# Word dosyalarını işlemek için gerekli modül
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$word = New-Object -ComObject Word.Application
$word.Visible = $false

# Excel dosyalarını işlemek için gerekli modül
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false

# Function to remove footer from Word document
Function Remove-WordFooter($filePath) {
$doc = $word.Documents.Open($filePath)
foreach ($section in $doc.Sections) {
foreach ($headerFooter in $section.Footers) {
$headerFooter.Range.Text = “”
}
}
$doc.Save()
$doc.Close()
}

# Function to remove footer from Excel document
Function Remove-ExcelFooter($filePath) {
$workbook = $excel.Workbooks.Open($filePath)
foreach ($worksheet in $workbook.Worksheets) {
$worksheet.PageSetup.LeftFooter = “”
$worksheet.PageSetup.CenterFooter = “”
$worksheet.PageSetup.RightFooter = “”
}
$workbook.Save()
$workbook.Close()
}

# Function to search and remove footers in a local directory
Function Search-And-Remove-Footers($path) {
$files = Get-ChildItem -Path $path -Recurse -Include *.docx, *.xlsx

foreach ($file in $files) {
try {
if ($file.Extension -eq “.docx”) {
Remove-WordFooter $file.FullName
} elseif ($file.Extension -eq “.xlsx”) {
Remove-ExcelFooter $file.FullName
}
} catch {
Write-Host “Error processing file: $file.FullName”
}
}
}

# Define the path to search
$localPath = “C:\Users\Ali.koc\Desktop\test”

# Search and remove footers for local files
Search-And-Remove-Footers $localPath

# Cleanup
$word.Quit()
$excel.Quit()





Running the Script

  1. Create the script in a text editor (e.g., Notepad) and save it as Remove-ClassificationFooter.ps1.
  2. Run PowerShell as an administrator.
  3. Navigate to the directory where the script is saved and run the script:

.\Remove-ClassificationFooter.ps1





if you want delete the specific footer, you can use script below

# Word uygulamasını başlat ve görünmez yap
$word = New-Object -ComObject Word.Application
$word.Visible = $false

# Belirli bir klasördeki tüm Word belgelerini işle
$folderPath = “C:\desktop”
$files = Get-ChildItem -Path $folderPath -Filter *.docx

foreach ($file in $files) {
Write-Host “Processing file: $($file.FullName)”
try {
# Her belgeyi aç
$document = $word.Documents.Open($file.FullName)

# Belirli bir metni alt bilgilerde ara ve sil
$textFound = $false
foreach ($section in $document.Sections) {
foreach ($footer in $section.Footers) {
$range = $footer.Range
$find = $range.Find
$find.Text = “!!!YOUR KEYWORD!!!”
$find.Replacement.Text = “”
$find.Forward = $true
$find.Wrap = 1 # wdFindContinue
$find.Format = $false
$find.MatchCase = $false
$find.MatchWholeWord = $true
$find.MatchWildcards = $false
$find.MatchSoundsLike = $false
$find.MatchAllWordForms = $false

$result = $find.Execute()
if ($result -eq $true) {
$textFound = $true
$range.Text = $find.Replacement.Text
Write-Host “Text found and replaced in footer of $($file.FullName)”
} else {
Write-Host “Text not found in footer of $($file.FullName)”
}
}
}

if ($textFound) {
# Değişiklikleri kaydet
$document.Save()
}
# Belgeyi kapat
$document.Close()
} catch {
Write-Host “Error processing file: $($file.FullName) – $_”
}
}

# Word uygulamasını kapat
$word.Quit()

# COM nesnelerini serbest bırak
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($word) | Out-Null
Remove-Variable word
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()





Conclusion

This script will automatically remove the “Classification” footer from all Word documents in the specified directory. This solution saves time and reduces the likelihood of errors.

By leveraging the power and flexibility of PowerShell, you can automate your document editing tasks and work more efficiently. Using PowerShell for tasks like this can greatly optimize your workflows.

Enjoy !

Language »