1

I'm still new to Powershell. Although my script claims success 100% of the time it's only effective maybe 1/2 to 2/3 of the time. I'mout what is happening or more accurately not happening. I've found and added the transcription feature but that only seems to give version information for the most part. Although I've tried a few different things, I'm missing why this only works about half the time.

I've included some of the resources I've used in building this script at the top. The script does the following: it'll check for admin' privileges, create variables to keep the code clean, export the current default applications to a remote location, and lastly import the default file associations that I've exported from another pc.

I've powered through many error messages and finally gotten it to a point where it works but sadly it's only intermittent. On each computer that I run this script it will go through all the motions and claim to be successful 100% of the time but in actuality maybe 1/2 to 1/3 of the time it doesn't actually change the default file associations. It will always export the initial file associations on the computer however.

<#
Explanation:    This powershell script will prompt for admin' priveleges
                then open a new powershell window. It will export the
                current file associations to a remote location and import the vetted file
                associations from the local location.

Source: https://techcommunity.microsoft.com/t5/ask-the-performance-team/how-to-configure-file-associations-for-it-pros/ba-p/1313151
Source: https://stackoverflow.com/questions/7690994/running-a-command-as-administrator-using-powershell
Source: https://www.tenforums.com/tutorials/8744-export-import-default-app-associations-new-users-windows.html
Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/remove-variable?view=powershell-7.3
#>


# If the Powershell script is not running in administrator mode this will start in administrator mode.
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
{  
  # Create variable for verbose output and start transcript
  $DAVerbose = "\\folder\Verbose-Test.txt"
  Start-Transcript -Path $DAVerbose -Append

  # Create variable. Grab hostname for xml filename creation
  $DAHName = hostname

  # Create variables for xml location and command to export XML
  $DAXMLPut = '\\folder\CustomFileAssoc-' +$DAHName +'.xml'
  $DAXportXML = "&dism.exe /online /export-defaultappassociations:$DAXMLPut"

  # Create variables for xml location and command to import XML 
  $DAXMLget = "C:\folder\DACustomFileAssoc.xml"
  $DAImportXML = "&dism.exe /online /Import-defaultappassociations:$DAXMLget"

  #Start powershell, export file associations and wait for completion, then import file associations
  Start-Process powershell -Verb runAs -ArgumentList $DAXportXML -Wait
  Start-Process powershell -Verb runAs -ArgumentList $DAImportXML -Wait

  # Face-value (Self-explanatory)
  Stop-Transcript

  # delete the variables and their contents
  Remove-Variable 'DA*'
  Break
  }

The transcription file only provides me with the following.

Windows PowerShell transcript start
Start time: 20230517104239
Username: AndrewH
RunAs User: AndrewH
Configuration Name: 
Machine: S1-LOUNGE1 (Microsoft Windows NT 10.0.19045.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\folder\PS-ExAndImpFileAssociations.ps1'
Process ID: 9336
PSVersion: 5.1.19041.2673
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.2673
BuildVersion: 10.0.19041.2673
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is \\folder\Verbose-Test.txt
**********************
Windows PowerShell transcript end
End time: 20230517104247
**********************
Andrew H.
  • 11
  • 4

0 Answers0