# Rewriting existing .htm signature file in Outlook folder

**URL:** <https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899>\
**Category:** PowerShell Help\
**Created:** [March 3, 2020, 7:26am UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899 "2020-03-03T07:26:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fizzler](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fizzler](https://forums.powershell.org/u/fizzler)\
**Post date:** [March 3, 2020, 7:26am UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/1 "2020-03-03T07:26:14Z")

</div>

Hello, I found this awesome script online to create an email signature in a users local Outlook. It is all working via a GPO, however, if I wanted to make any changes to the html or the script on the server to change the .htm being created, I would have to go into each user’s appdata folders and delete the file so it would be recreated the next time they logon. Can someone help with what I need to add to this script for it to overwrite/replace the current file in the appdata/user/roaming/MSFT/signatures folder folder every time script is run?

```
# Gets the path to the user appdata folder $AppData = (Get-Item env:appdata).value # This is the default signature folder for Outlook $localSignatureFolder = $AppData+'\Microsoft\Signatures' # This is a shared folder on your network where the signature template should be $templateFilePath = "C:\Users\\Desktop\template.html" # Get the current logged in username $userName = $env:username # The following 5 lines will query AD and get an ADUser object with all information $filter = "(&(objectCategory=User)(samAccountName=$userName))" $searcher = New-Object System.DirectoryServices.DirectorySearcher $searcher.Filter = $filter $ADUserPath = $searcher.FindOne() $ADUser = $ADUserPath.GetDirectoryEntry() # Now extract all the necessary information for the signature $name = $ADUser.displayName $email = $ADUser.mail $job = $ADUser.description $department = $ADUser.department $phone = $ADUser.telephonenumber $address = $ADUser.streetaddress $website = $ADUser.wWWHomePage $city = $ADUser.l $mobile = $ADUser.mobile $state = $ADUser.st $postcode = $ADUser.postalCode $namePlaceHolder = "DISPLAY\_NAME" $emailPlaceHolder = "EMAIL" $jobPlaceHolder = "JOB\_TITLE" $departmentPlaceHolder = "DEPARTMENT" $phonePlaceHolder = "PHONE" $addressPlaceHolder = "ADDRESS" $websitePlaceHolder = "WEBSITE" $cityPlaceholder = "SUBURB" $mobilePlaceholder = "MOBILE" $statePlaceHolder = "STATE" $postcodePlaceHolder = "POSTCODE" $rawTemplate = get-content $templateFilePath"\template.html" $signature = $rawTemplate -replace $namePlaceHolder,$name $rawTemplate = $signature $signature = $rawTemplate -replace $emailPlaceHolder,$email $rawTemplate = $signature $signature = $rawTemplate -replace $phonePlaceHolder,$phone $rawTemplate = $signature $signature = $rawTemplate -replace $jobPlaceHolder,$job $rawTemplate = $signature $signature = $rawTemplate -replace $departmentPlaceHolder,$department $rawTemplate = $signature $signature = $rawTemplate -replace $websitePlaceHolder,$website $rawTemplate = $signature $signature = $rawTemplate -replace $cityPlaceHolder,$city $rawTemplate = $signature $signature = $rawTemplate -replace $mobilePlaceHolder,$mobile $rawTemplate = $signature $signature = $rawTemplate -replace $statePlaceHolder,$state $rawTemplate = $signature $signature = $rawTemplate -replace $postcodePlaceHolder,$postcode $rawTemplate = $signature $signature = $rawTemplate -replace $addressPlaceHolder,$address # Save it as \<username\>.htm $fileName = $localSignatureFolder + "\" + $userName + ".htm" # Gets the last update time of the template. if(test-path $templateFilePath){ $templateLastModifiedDate = [datetime](Get-ItemProperty -Path $templateFilePath -Name LastWriteTime).lastwritetime } # Checks if there is a signature and its last update time if(test-path $filename){ $signatureLastModifiedDate = [datetime](Get-ItemProperty -Path $filename -Name LastWriteTime).lastwritetime if((get-date $templateLastModifiedDate) -gt (get-date $signatureLastModifiedDate)){ $signature \> $fileName } }else{ $signature \> $fileName }
```

---

<div class="post-metadata">

**Author:** ![fizzler](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fizzler](https://forums.powershell.org/u/fizzler)\
**Post date:** [March 12, 2020, 7:24am UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/2 "2020-03-12T07:24:32Z")

</div>

Anyone know if this is possible with the above script?

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [March 12, 2020, 1:35pm UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/3 "2020-03-12T13:35:26Z")

</div>

At first glance, it looks like you are already doing that with

$signature \> $fileName

It also looks like there have been two edits other than yourself … maybe they fixed it for you?

&nbsp;

---

<div class="post-metadata">

**Author:** ![fizzler](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fizzler](https://forums.powershell.org/u/fizzler)\
**Post date:** [March 15, 2020, 4:17pm UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/4 "2020-03-15T16:17:14Z")

</div>

[quote quote=209994]At first glance, it looks like you are already doing that with

$signature \> $fileName

It also looks like there have been two edits other than yourself … maybe they fixed it for you?

[/quote]

I think the edits were mainly for presentation. I’m sure the code is exactly the same.

The script only creates a signature if the signature folder is empty. If there are changes to the html template I have to ask everyone to delete their signature and then log on and off again. I would prefer a script that overwrites the file.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [March 16, 2020, 9:21am UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/5 "2020-03-16T09:21:54Z")

</div>

Maybe try Out-File with Force

`$signature | Out-File -FilePath $fileName -Force`

&nbsp;

---

<div class="post-metadata">

**Author:** ![fizzler](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fizzler](https://forums.powershell.org/u/fizzler)\
**Post date:** [March 18, 2020, 4:13pm UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/6 "2020-03-18T16:13:46Z")

</div>

Thanks Tony!! It didn’t work in my test, unfortunately. Still won’t override the current signature file.

I assume you mean replacing $signature \> $fileName with your above code?

Edit: What I did was replace everything under line 122 with this:

`# Save it as .htm`  
`$fileName = $localSignatureFolder + "&quot; + $userName + ".htm"`

`$signature | Out-File -FilePath $fileName -Force`

It actually works. I’m wondering now. Does this mean that the code removed is not important to the function of the script:

`# Gets the last update time of the template.`  
`if(test-path $templateFilePath){`  
`$templateLastModifiedDate = [datetime](Get-ItemProperty -Path $templateFilePath -Name LastWriteTime).lastwritetime`  
`}`

`# Checks if there is a signature and its last update time`  
`if(test-path $filename){$signatureLastModifiedDate = [datetime](Get-ItemProperty -Path $filename -Name LastWriteTime).lastwritetime`  
`if((get-date $templateLastModifiedDate) -gt (get-date $signatureLastModifiedDate)){`  
`$signature > $fileName`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/rewriting-existing-htm-signature-file-in-outlook-folder/13899/7 "2024-05-16T20:30:36Z")

</div>

Closed on May 16, 2024, 8:30pm UTC


