Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
March Scripting Games Advanced Diacritics script and Scheduled task.
Scans the file names for Diacritics letters beloning to the Latin-1 Supplement.
Scans the file names for Diacritics letters beloning to the Latin-1 Supplement.
Before use check that valid values for the -Path, $Mailserver,$From and $To are correct.
function Get-Diacritic
# Location of files Driveletter\folder
ValueFrompipeline = $true,
[ValidateScript({Test-Path $_ -PathType Container})]
# E-mail Parameters
[String]$MailServer = '',
[string]$From = '',
[String]$To = ''
# Get todays date.
$Date = Get-Date -Format yyyy-MM-dd
# Generate CSV file name including todays date.
$CSV = $Date + "_FileNamesWithDiacritics.csv"
# Name including todays date for error log if needed.
$Errorlog = 'Diacritics' + $date + '.log'
# configure Font size and color for Email HTML content.
$head = @'
h1 {color: #0000FF;
font-size: 12pt}
h2 {color: #FF0000;
font-size: 12pt}
Body{font-size: 12pt}
# function to format file size
Function format-size($length){
if ($length -gt 1GB){('{0:n1}' -f ($length /1GB)) + 'GB'}
elseif($length -gt 1MB){('{0:n1}' -f ($length /1MB)) + 'MB'}
elseif($length -gt 1KB){('{0:n1}' -f ($length /1KB)) + 'KB'}
else{('{0:n1}' -f ($length )) + 'B'}
# scan for files with Diacritics in file name
$files = Get-ChildItem -path $path -Recurse -force |
Where {$_.Name -match '[\u00C0-\u00ff]' -and $_.Name -notmatch '[\u00d7] | [\u00f7]'} |
select Name,
@{N ='Creation Date';E = {$_.Creationtime}},
@{N ='Last Modification Date'; E = {$_.LastWriteTime}},
@{N = 'File Size'; E = {Format-size $_.Length}} #| format-table -AutoSize
# if files found with Diacritics save results in CSV file and generate the content of email in HTML format
If($files.count -gt 0){
$total = $files.count
$files | Export-Csv -Path $env:temp\$CSV
$Message = $files | ConvertTo-Html -Head $head -body $_ -as Table -PostContent "<H2>Generated using Powershell</H2>" `
-Precontent "<H1>Scan Date $Date : Files with Diacritics found under C:\FileShare: $total files in total </H1>" |
# if no files found generate email content in HTML format
else {$message = $Message = $files | ConvertTo-Html -Head $head `
-body "<H1>Scan Date $Date : Files with Diacritics found under C:\FileShare </H1>",
"Scan completed successfully no files found today.", "<H2>Generated using Powershell</H2>" |
# Attempts to send Email or generates a log file in the temp folder.
$ErrorActionPreference = 'stop'
$params = @{SmtpServer = $MailServer
From = $From
To = $To
Subject = "Files with Diacritics Marks"
Body = $Message
BodyAsHtml = $true}
Send-MailMessage @params
$_ | out-file -FilePath $env:temp\$Errorlog
# calls the Get-Diacritic function and provides path to the files
Get-Diacritic -Path C:\fileshare
# set valid values for target host machine, the username and password befor use.
Invoke-Command FileServer {Register-ScheduledTask -TaskName Diacritics `
-Description 'List all files using letters from Latin-1 Supplement in filenames' -user 'AccountName' -Password '**********' `
-Action (New-ScheduledTaskAction Powershell.exe '-NoProfile -NonInteractive -command "& C:\scripts\Get-Diacritics.ps1"')`
-Trigger (New-JobTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Saturday -At 11pm )}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment