automatic deletion of temporary files contents in Windows machine - powershell

I am using Windows XP. Now i am having some issue which causing my PC to run slow. So very frequently i was need to go and delete some temporary files from the directory C:\WINDOWS\Temp . Any way to get it done with a script automatically?
Error
I get the below error when I tried to run the script from my machine:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\rakshiar>D:\WIPData\cleanup.ps1
C:\Documents and Settings\rakshiar>powershell.exe D:\WIPData\cleanup.ps1
File D:\WIPData\cleanup.ps1 cannot be loaded because the execution of scripts i
s disabled on this system. Please see "get-help about_signing" for more details
.
At line:1 char:23
+ D:\WIPData\cleanup.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
C:\Documents and Settings\rakshiar>
Thanks

you have to change your executionPolicy ( by default powershell not allow to run script), so strart powershell as administrator and run :
Set-ExecutionPolicy remotesigned

I use this powershell script to clean up a folder on my windows machine. It's run via scheduled tasks; use something like this as the command "powershell.exe &'c:\psbin\cleanup.ps1'".
$dir = "C:\SomeDirectory"
$d = [System.DateTime]::Now.AddDays(-1)
gci -recurse $dir | ?{ !$_.psiscontainer -and $_.LastWriteTime -lt $d } | %{ remove-item -Force $_.FullName }
This deletes files older than one day. Change the AddDays to change this.

Related

remove dell optimizer service - powershell

Problem -
Unable to uninstall dell optimizer service
Long version -
i am looking to write what should be a very basic script for removing dell optimizer. when its fully installed and i run get-package I get the following.
When this is uninstalled by piping into uninstall-package. we are still left with dell optimizer service.
this is after stopping the services as well.
PS C:\WINDOWS\system32> Get-Package -Name *optimizer*
Name Version Source ProviderName
---- ------- ------ ------------
Dell Optimizer 2.0.651.0 C:\Program Files\Dell\DellOpt... msi
DellOptimizerUI 2.0.651.0 C:\Program Files (x86)\Dell\D... msi
Dell Optimizer Service 2.0.651.0 Programs
dell optimizer service in programs and features
the uninstall string as below when run does not accept -remove or -runfromtemp HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
"C:\Program Files (x86)\InstallShield Installation Information{286A9ADE-A581-43E8-AA85-6F5D58C7DC88}\DellOptimizer.exe" -remove -runfromtemp
+ ... A9ADE-A581-43E8-AA85-6F5D58C7DC88}\DellOptimizer.exe" -remove -runfro ...
+ ~~~~~~~
Unexpected token '-remove' in expression or statement.
At line:1 char:199
+ ... -A581-43E8-AA85-6F5D58C7DC88}\DellOptimizer.exe" -remove -runfromtemp
+ ~~~~~~~~~~~~
Unexpected token '-runfromtemp' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
the exe is located at
C:\Program Files (x86)\InstallShield Installation Information{286A9ADE-A581-43E8-AA85-6F5D58C7DC88}\DellOptimizer.exe
I have used 7zip to unpackage the .exe to see if there was an uninstall file but to no avail.
I believe it has something to do with its providername being program and also the dell optimizer service does not appear when you search for the below in ciminstance.
Im unsure how to find which CIMclass it would fall into as it could be a step in the right direction
PS C:\WINDOWS\system32> Get-CimInstance -ClassName Win32_Product | Sort-Object
Has anyone done something like this before? or anything i could try?
i worked it out. it was a silly mistake and needed to run the uninstall string in command prompt
#stop dell optimizer services
Get-Service -Name "DellOptimizer" | Stop-Service
#Get Dell Optimizer from registry
$uninstallstring = get-itemproperty 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Where-Object { $_.DisplayName -match "optimizer service"}
#get the uninstall string for dell optimizer
$uninstallstring = $removeapp.UninstallString
#add the silent parameter and run throguh cmd to uninstall
cmd /c $uninstallstring -silent
fix for your script
#stop dell optimizer services
Get-Service -Name "DellOptimizer" | Stop-Service
#Get Dell Optimizer from registry
$uninstallstring = get-itemproperty 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Where-Object { $_.DisplayName -match "optimizer service"}
#get the uninstall string for dell optimizer
$uninstallstring = $uninstallstring.UninstallString
#add the silent parameter and run throguh cmd to uninstall
cmd /c $uninstallstring -silent
why not just follow their documentation and download the exe. extract it to get the DellOptimizer.exe package it up with a simple powershell script that run DellOptimizer.exe /remove /silent

How to fix powershell after installing VS Code

I already was asking a similar question here: https://superuser.com/questions/1613630/most-of-powershell-commands-not-working?noredirect=1
Basically, I wasn't able to fix the issue so I got a replacement computer. Brand new. Started setting it up and only did the following:
Installed VS Code
Turned on Powershell extension in VS Code
It prompted something about Package Manager needing an update or it will not work properly
Asked me to install NuGet to get the Package Manager
After that PowerShell 5.1 stopped working again.
Modules not autoloading, fresh instance of powershell does this:
PS C:\WINDOWS\system32> Write-Host
Write-Host : The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:1
+ Write-Host
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any ideas?
This fixed it for me:
(Get-Module -ListAvailable).Name | %{Import-Module -Name $_ -Force}
[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Install-Module -Name PackageManagement -Force -RequiredVersion 1.4.6 -Scope AllUsers -AllowClobber -Verbose
Then if the last command doesn't install NuGet do this:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

How to resolve the Add-PSSnapin Microsoft.TeamFoundation.PowerShell and Get-TfsChildItem error?

Below is the PowerShell Script that I am using to download the folder from TFS 2013 Update 4 to my local machine but i am getting some exception which I have copied below the code.
$AutoDeployDir = "$/Intel/Installscript/Utility Scripts"
$deployDirectory = "C:\powershell scripts\New folder\Demo TFS Code"
# Add TFS 2013 dlls so we can download some files
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
$tfsCollectionUrl = "http://stptfs2013dbsvr:8080/tfs/intelcollection"
$tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionUrl
$tfsVersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
# Register PowerShell commands
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
# Get all directories and files in the AutoDeploy directory
$items = Get-TfsChildItem $AutoDeployDir -Recurse -Server $tfsCollection
# Download each item to a specific destination
foreach ($item in $items) {
# Serverpath of the item
Write-Host "TFS item to download:" $($item.ServerItem) -ForegroundColor Blue
$destinationPath = $item.ServerItem.Replace($AutoDeployDir, $deployDirectory)
Write-Host "Download to" $([IO.Path]::GetFullPath($destinationPath)) -ForegroundColor Blue
if ($item.ItemType -eq "Folder") {
New-Item $([IO.Path]::GetFullPath($destinationPath)) -ItemType Directory -Force
}
else {
# Download the file (not folder) to destination directory
$tfsVersionControl.DownloadFile($item.ServerItem, $([IO.Path]::GetFullPath($destinationPath)))
}
}
Exception that occurs is:
Add-PSSnapin : The Windows PowerShell snap-in 'Microsoft.TeamFoundation.PowerShell' is not installed on this computer.
At C:\powershell scripts\demo1.ps1:12 char:1
+ Add-PSSnapin Microsoft.TeamFoundation.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.TeamFoundation.PowerShell:String) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
Get-TfsChildItem : The term 'Get-TfsChildItem' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\powershell scripts\demo1.ps1:15 char:10
+ $items = Get-TfsChildItem $AutoDeployDir -Recurse -Server $tfsCollection
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-TfsChildItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Please make sure you have fully installed the TFS Powertools.
By default it doesn’t install the PowerShell CmdLets. If it is, just install it and then the issue will be gone.
Another possibility is that the PowerShell Snap-in is stored in the inconsistent registry with the OS version (32bit/64bit).
PowerTools installer is 32bit, on 64bit machine, it will write to
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\PowerShellSnapIns,
but not to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns.
Reference this similar thread for details: TFS Power Tools 2008 Powershell Snapin won’t run in on 64-bit in Windows 2008 R2?

TFS Powershell Install-Module not found

I've written the PS script below to 7zip a folder using 7Zip4Powershell however when during the build it fails with the following error:
Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Build\Work\19\s\DevOps\7zCompress.ps1:3 char:5
+ Install-Module -Scope CurrentUser -Name 7Zip4PowerShell -Verbose -Force
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Install-Module:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Compress-7Zip : The term 'Compress-7Zip' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Build\Work\19\s\DevOps\7zCompress.ps1:18 char:1
+ Compress-7Zip -Path $sourcedir -ArchiveFileName $filename -CompressionLevel $com ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Compress-7Zip:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Process completed with exit code 0 and had 1 error(s) written to the error stream.
Script
param([string]$sourcedir, [string]$filename, [string]$compressionLevel, [string]$compressionMethod)
If (-not (Get-Module -ListAvailable -Name 7Zip4PowerShell)) {
Install-Module -Scope CurrentUser -Name 7Zip4PowerShell -Verbose -Force
}
If(Test-Path $filename) {
Remove-Item $filename
}
If (-not $compressionLevel) {
$compressionLevel = "Normal"
}
If (-not $compressionMethod) {
$compressionMethod = "Lzma2"
}
Compress-7Zip -Path $sourcedir -ArchiveFileName $filename -CompressionLevel $compressionLevel -CompressionMethod $compressionMethod
How do I get TFS 2015 On-Premises to recognize the Install-Module is a valid command? I've already installed the MSI for Install-Module on that machine. Do I perhaps need to add it as a build agent capability?
According to your error message , double check your powershell version in the build agent machine.
Cause from Powershell 5.0 onwards you , you will be able to use the cmdlet to Install-Module, Save-Module. If you are using the lower version of PS, will get similar error.
You could also manually RDP to the build agent with the build service account and run the PS script. This will narrow down if the issue is related to the environment or your build definition.

WAPPSCmdlets is not installed on this machine

I have a Windows 2008 R2 machine, and I have installed the VS2010 and SP1, Windows Azure SDK 1.7 and all other tools of Azure SDK and PowerShell cmdlets from the below links
https://www.windowsazure.com/en-us/manage/downloads/
When I run the Windows Powershell for Windows Azure Cmdlets, it is showing the error:
Add-PSSnapin : The Windows PowerShell snap-in 'WAPPSCmdlets' is not installed o
n this machine.
At line:1 char:21
+ cd c:\; add-pssnapin <<<< WAPPSCmdlets
+ CategoryInfo : InvalidArgument: (WAPPSCmdlets:String) [Add-PSSn
apin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
dPSSnapinCommand
I have also tried the following link but no use.
http://social.msdn.microsoft.com/Forums/is/windowsazuredevelopment/thread/4c59fc7e-3ead-482e-88c3-f5555e915c84
Could you try loading the module manually?
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1"
And then simply try any CmdLet (like Add-AzureCertificate) to see if you can use it.
Update: Since Add-AzureCertificate works for you this means all cmdlets are now available. To make things easier you can simply create a shortcut on your desktop with the following target:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NoExit
-Command "cd 'C:\'; Get-ChildItem 'C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure*.psd1' | ForEach-Object
{Import-Module $_}"