Forum Discussion

Rambo363636's avatar
Rambo363636
Brass Contributor
Jul 19, 2024

Need a powershell script to delete a word document on intune devices

Hi All,

 

I am trying to delete a word document on all intune devices. I have tried below but did not work.
Get-ChildItem -Path "%appdata%\Microsoft\usertemplates" -Filter "WordTemplate-2.dotm" -Recurse | Remove-Item
Get-ChildItem -Path "%appdata%\Microsoft\usertemplates" -Filter "WordTemplate-2.dotx" -Recurse | Remove-Item

Could you please assist me providing a full powershell script that will work and remove the above files at above location on all the intune devices remotely.

Thanks.

 

  • Rambo363636 

     

    hope the following works for you 

     

    # Define the file paths and file names
    $templatePath = "$env:APPDATA\Microsoft\usertemplates"
    $templateFiles = @("WordTemplate-2.dotm", "WordTemplate-2.dotx")
    
    # Loop through each file and attempt to delete it
    foreach ($file in $templateFiles) {
        $fullPath = Join-Path -Path $templatePath -ChildPath $file
        if (Test-Path -Path $fullPath) {
            Remove-Item -Path $fullPath -Force -ErrorAction SilentlyContinue
            Write-Host "Deleted: $fullPath"
        } 
    }
  • sdtslmn's avatar
    sdtslmn
    Brass Contributor

    Rambo363636 

     

    hope the following works for you 

     

    # Define the file paths and file names
    $templatePath = "$env:APPDATA\Microsoft\usertemplates"
    $templateFiles = @("WordTemplate-2.dotm", "WordTemplate-2.dotx")
    
    # Loop through each file and attempt to delete it
    foreach ($file in $templateFiles) {
        $fullPath = Join-Path -Path $templatePath -ChildPath $file
        if (Test-Path -Path $fullPath) {
            Remove-Item -Path $fullPath -Force -ErrorAction SilentlyContinue
            Write-Host "Deleted: $fullPath"
        } 
    }

Resources