How to launch Microsoft Teams via PowerShell

Hygge Jack 1 Reputation point
2021-03-02T23:02:30.99+00:00

Hi All! I'm creating a script that will clean the Microsoft Teams cache files. The script works by closing Teams, deleting all of the files except two folders, then (and this is where I'm having problems)...launch Microsoft Teams.

I also noticed that if Teams was already closed, it generates an error. I'm not sure if that can be fixed or now.

I've tried this Start-Process -File C:\Users\%username%\AppData\Local\Microsoft\Teams\Update.exe --process "Teams.exe" but this errors out.

Can someone help me with the code so that at the end it launches Microsoft Teams?

Here's the code I have:

   Stop-Process -Name Teams -Force
   $TeamsPath = $env:APPDATA+"\Microsoft\TeamsTest\"
   Get-ChildItem -Path $TeamsPath -Exclude "Backgrounds", "meeting-addin" | foreach ($_) {
       "CLEANING :" + $_.fullname
       Remove-Item $_.fullname -Force -Recurse
       "CLEANED... :" + $_.fullname
   }   
   Start-Process -File C:\Users\%username%\AppData\Local\Microsoft\Teams\Update.exe --process "Teams.exe"

And here are the errors the last line generates:

Stop-Process : Cannot find a process with the name "Teams". Verify the process name and call the cmdlet again.
At line:1 char:4
+    Stop-Process -Name Teams -Force
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Teams:String) [Stop-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.StopProcessCommand

Start-Process : A positional parameter cannot be found that accepts argument 'Teams.exe'.
At line:8 char:4
+    Start-Process -File C:\Users\%username%\AppData\Local\Microsoft\Te ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

Thank you so much for looking at this with me!

~ Jack

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-03-03T03:28:13.897+00:00

    See if this works for you:

    # Stop Teams
    Get-Process "Teams" -ErrorAction SilentlyContinue | Stop-Process
    #
    # Do what you need to here
    #
    # change to the correct directory
    Set-Location ($ENV:USERPROFILE + '\AppData\Local\Microsoft\Teams')
    # start Teams
    Start-Process -File "$($env:USERProfile)\AppData\Local\Microsoft\Teams\Update.exe" -ArgumentList '--processStart "Teams.exe"'
    
    4 people found this answer helpful.

  2. Aasim Pathan 6 Reputation points
    2021-06-18T06:56:10.59+00:00

    Actually, all you need is a simple one-liner

    Start-Process -File $env:LOCALAPPDATA\Microsoft\Teams\Update.exe -ArgumentList '--processStart "Teams.exe"'

    1 person found this answer helpful.

  3. HelpDeskGuy75 1 Reputation point
    2022-05-19T14:06:19.55+00:00

    This was a great help thank you for this answer AasimPathan-9021

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.