File Management

This article is about PowerShell and the tasks I use it for.

Remove a file

PS C:\> Get-ADComputer -Filter *|Select-Object -ExpandProperty name|foreach { Write-Output "$_"; if(Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 ){ Invoke-Command -Script {if (Test-Path '\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\StupidToHaveHereAnyway.lnk'){remove-item '\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\StupidToHaveHereAnyway.lnk'}} -ComputerName $_}}

 

Push a copy of a file

PS C:\> Get-ADComputer -Filter { OperatingSystem -like "Windows 10*" } -Properties OperatingSystem |Select-Object -ExpandProperty name|foreach { Write-Output "$_"; if(Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 -Quiet ){ robocopy "\\servername\share\path" "\\$_\c`$\folder\target" filename.chm } else { Write-Output "Couldn't connect to $_" } }

Clean old profiles off of a workstation

#First look at the targets:

Get-CimInstance -ClassName win32_userprofile -ComputerName 'thiscomputername'|Select-Object -Property 'localpath'

#Then de-select any that you want to leave:

Get-CimInstance -ClassName win32_userprofile -ComputerName 'pc1'|Where-Object {($_.LocalPath.split('\')[-1] -ne 'jdoe' -and $_.LocalPath.split('\')[-1] -ne 'jsmith' -and $_.LocalPath.split('\')[-1] -ne 'pcadmin' -and $_.LocalPath.split('\')[-1] -ne 'pcadmin.MYDOMAIN'  -and $_.LocalPath.split('\')[-1] -ne 'NetworkService' -and $_.LocalPath.split('\')[-1] -ne 'LocalService' -and $_.LocalPath.split('\')[-1] -ne 'systemprofile' -and $_.LocalPath.split('\')[-1] -ne 'admin.xyz');}|Remove-CimInstance