Remove all microsoft teams .exe references from roaming profiles - powershell

I have set up a Microsoft Teams GPO deployment using a .msi.
Unfortunately some users have already installed teams using the .exe installer.
This then creates issues with the .msi deployment as when the GPO sees the .exe files it does not apply.
Does anyone have a script that will delete all references to teams.exe within a users roaming profile?
Many thanks

This command will list all files with full path in Users' AppData\Roaming\Microsoft\Teams folder (recursive) and will delete all files that match with "\teams.exe"
Get-ChildItem "C:\Users\*\AppData\Roaming\Microsoft\Teams\*" -Recurse | ForEach{if($_.FullName -match '\\teams.exe'){Remove-Item "$($_.FullName)" -Force -WhatIf}}
Note: I added "-WhatIf" at the end of the command to avoid any mistake before running the command for real. So you have to remove "-WhatIf" after checking the result with "-WhatIf"

Related

Powershell script for uninstall Software

I need help with powershell script I need create script for deployment for many workstations which I push to the machines via deployment tool. I need uninstall app AzInfoProtection.exe the problem is that the path for this SW is different for each computer. So I don´t know I need find path by the executable file and after this create the command to uninstall it.
app name: AzInfoProtection.exe
Path somewhere: C:\ProgramData\Package
Cache**\
I found this but I don´t know how can I get from the output the path
Get-ChildItem -Path "C:\ProgramData\Package Cache" -Filter AzInfoProtection.exe -Recurse -ErrorAction SilentlyContinue -Force
So I need script that will uninstall this SW on many computer and the command for uninstalltion should be:
"C:\ProgramData\Package Cache{ca644579-3d97-4c24-8bf0-a4ccd297b6a6}\AzInfoProtection.exe" /uninstall /quiet
this part is fro each computer different {ca644579-3d97-4c24-8bf0-a4ccd297b6a6} so I need script which find it.
I will be glad if you help me,
thanks

Can delete file with Windows GUI but not with powershell, both using administrator right

I'm trying to delete a machinekey in the folder "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys" via powershell, but get met with the error "You do not have permmision to delete this file.
If I try to delete the same file via Windows gui I'm allowed to do it.
It's the same Admin account I'm using.
This is the code I'm using.
Get-ChildItem -Path "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\19c5cf9c*" | Remove-Item -Force

Jenkins job to Delete TeamCity's work directory

I am writing PowerShell scripts to delete TeamCity work directory on daily basis, the script is working well in ideal condition(when none of the folder in work directory is in use). But when any build is executing in TeamCity and during that time if I execute the script, it shows failure.
#Cleanup work directory of TeamCity's build agent
$Path = "C:\BuildAgent\work"
$exclude = #("*.old", "*directory.map")
Get-ChildItem $Path -Exclude $exclude | Remove-Item -Recurse
Can anyone suggest how can I skip this particular condition without showing any failure so that all the files are deleted except the one which is in use.
There is an undocumented API call that allows you to clean sources on a particular agent.
Loop through all agents and make a POST request to:
http://teamcity/ajax.html?resetSources=<agentID>
Assuming you are using a new version of TeamCity, you will need to be aware of CSRF protection.

Export-Csv - Access to the path 'C:\export.csv' is denied

I'm getting the information I need out of the line, although when I go to export the information, I get an error.
When I run this code:
get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
I get this error:
Export-csv : Access to the path 'C:\export.csv' is denied.
At line:1 char:14
+ get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How can I export to CSV?
You need to use an admin console or change the location of the csv file to C:\temp or something. The home drive require admin permissions.
EDIT:
The elevation requirement is only with system directories like:
C:\
C:\Windows
C:\Program Files
C:\Program Files (x86)
C:\Users
Notice that you can change C:\Users\<YourUserName> but not any other directories under C:\Users. Windows was built for multiple users even if you may be the only one using it. The above directories are common for all users and changing its contents would effectively change the Windows environment for other users too since windows rely on the integrity of these directories. Allowing a normal user to delete/modify/add files to these could have dangerous results for the OS and other people who use it.
So it tries to makes sure that only an Admin user is authorized to make changes to it. It is also the same reason why you get prompted for admin permissions when installing a software. It is installed to C:\Program Files or C:\Program Files (x86), make changes to the registry and is available to all users of that computer. Same with removing it. If a program does not require to do that, it doesn't require admin privileges. Eg: Portable softwares. Some programs only use the user directory or users temp directory C:\Users\<Username>\AppData\Local\Temp. Such programs are only available to the user that installed it.
If you have a custom directory like C:\myGames, adding files to it wont require elevation. So C:\Temp is just another directory like that. You will also notice that C:\ will let a normal user create a folder but not a file.
SUMMARY: Any changes that affect other user directories or the windows environment will require admin rights. Others don't.
In my case, the error occurred because I failed to specify the file name. The correct syntax is;
Export-Csv -path ".\targetList.csv"
and not -path "."

Copy-Item script creating files on google drive that I cannot delete

I am trying to create a new folder on google drive and append the date and hour to the end of it to create a rolling 1 hour backup with old versions still accessible. I am not knowledgeable enough about PowerShell in order to automatically delete folders over 7 days old so I have to do it manually. The problem seems to be when PowerShell creates the new folders and copies the files, I no longer have access to delete them. I have checked permissions and added Everyone, Administrators, and the current user (the only user and admin) and it still will not allow me to delete the folders that PowerShell creates. I can delete files I manually put into Google drive. Here is my code:
$yest = (Get-Date).AddDays(-1).ToString(‘hhMMddyy’)
mkdir "C:\Users\admin\Google Drive\SpaceEngineersDedicated.$yest" -Force
Copy-Item C:\Users\admin\AppData\Roaming\SpaceEngineersDedicated -Destination "C:\Users\admin\Google Drive\SpaceEngineersDedicated.$yest" -Recurse -Force
If someone could help me with code that will just delete folders over 7 days, I would appreciate it but worst case scenario I'm fine with occasionally cleaning them up manually.