I am needing to create a powershell script that will pull down the latest packages from our nuget feed. This script will run during a TeamCity build to help with a caching issue that we are having with the nuget packages. I have not worked with powershell before, so any help would be greatly appreciated. This is what I have so far:
$packagesConfigFiles = Get-ChildItem $path -Recurse | Where-Object {$_.Name -eq "packages.config"}
foreach($packagesConfig in $packagesConfigFiles){
.\nuget.exe i $packagesConfig.FullName -o Source\Packages
}
Just in case someone needs it I wanted to post what I used for my solution.
Set-ExecutionPolicy Unrestricted
$packagesConfigFiles = Get-ChildItem $path -Recurse | Where-Object {$_.Name -eq "packages.config"}
if (Test-Path packages){
Write-Host "Delete packages folder."
Remove-Item packages -recurse
}
foreach($packagesConfig in $packagesConfigFiles){
.nuget\NuGet.exe i $packagesConfig.FullName -o packages
}
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
Related
I'm working on removing bloatware that is preinstalled on a number of computers.
I've been able to create a small script to remove the items that are preinstalled from the Microsoft Store and one that uninstalls Teams completely.
However; I'm having some troubles creating a solid script to uninstall OneDrive completely.
So far I have the below:
#Instructions found on https://www.wintips.org/how-to-disable-uninstall-install-onedrive-in-windows-10-8-7/]
#Modified slightly for simplicity and to kill the OneDrive process before uninstallation of application
#To Kill OneDrive.exe process
taskkill /f /im OneDrive.exe
#To uninstall OneDrive if using 64-bit System:
C:\windows\SysWOW64\OneDriveSetup.exe /uninstall
#To uninstall Onedrive if using a 32-bit system:
C:\windows\System32\OneDriveSetup.exe /uninstall
#Added to Removes the OneDrive Folders that are on the laptop.
$dirpath = "C:\Users\$env:UserName\OneDrive"
$dirpath2 = "C:\Users\$env:UserName\OneDrive - CompanyName"
#conditional to delete OneDrive related folders of C Drive. This is where I run into trouble
if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}
#Remove-Item -LiteralPath "C:\Users\$env:UserName\OneDrive" -Force -Recurse
#Remove-Item -LiteralPath "C:\Users\$env:UserName\OneDrive - CompanyName" -Force -Recurse
exit
It seems that there might be a logic issue with my conditional statement. When I run this script it does delete both folders that I'm intending to delete, but it returns "False" instead of "True" as I would expect.
I think what is happening is that it is running the remove-Item -LiteralPath $dirpath portion before it is able to reach the logical operator. I'm under this impression, because if I use the -and operator it will only remove the first Folder "C:\Users\$env:UserName\OneDrive"
Any suggestions to resolve this issue or improve the script overall would be appreciated. Thank you.
You should use a foreach
$dirpaths = "C:\Users\$env:UserName\OneDrive", "C:\Users\$env:UserName\OneDrive - CompanyName"
Foreach ($dirpath in $dirpaths) {
if (test-path -LiteralPath $dirpath) {remove-Item -LiteralPath $dirpath}
}
if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}
That logic is broken.
Try this:
if (test-path -LiteralPath $dirpath) {
Remove-Item -LiteralPath $dirpath
}
elseif (test-path -LiteralPath $dirpath2){
remove-Item -LiteralPath $dirpath2
}
else {
Write-Error -Message "We ain't found shit!"
}
Lastly
You don't need to do any of that because Remove-Item does not stop if it cannot find a file/path/directory - it continues.
Remove-Item c:\thisisafakepath\, e:\anotherfakepath\, C:\Powershell\TestCSVs\out.csv -WhatIf -ErrorAction SilentlyContinue
Write-Host "Script continues..."
Outputs:
C:> . 'C:\Powershell\Scripts\trydelete.ps1'
What if: Performing the operation "Remove File" on target "C:\Powershell\TestCSVs\out.csv".
Script continues...
I am trying to upgrade an application using an .msp file but every time I run the script I get a message saying the product I'm trying to upgrade from does not exist. My thought was there might be a bug in the original MSI so I decided to uninstall the application and a full install to the new version. When I run the script to Uninstall it works on some machines but not on others. When I try to uninstall using powershell it fails. When I use MSIEXEC /X I get the following message
"This action is only valid for products that are currently installed".
Next I decided to try and repair using MSIEXEC /fa and I get this:
"This installation package cannot be opened. Verify that the package exists and that you can access it, or contat the application vendor to verify tat this is a valid Winds Installer package"
The strange thing is I can uninstall the application with no problems from Add\Remove programs with not issues and install the upgrade. The problem with this is that I have over 3000 users. From SCCM, I tried to supercede the old version to uninstall before installing the new version and that also failed. Any help you guys can provide would be helpful. The application is Taxprep Forms 2021.1.0 upgrading to 2021.2.0. i have added 2 of the scripts I tried.
For this script I tried doing a repair first then uninstall. I also tried using both product code and app name:
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass
$uninstall32 = gci “HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\” | foreach { gp $_.PSPath } | ? { $_ -match “Taxprep Forms 2021” } | select UninstallString
$uninstall64 = gci “HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\” | foreach { gp $_.PSPath } | ? { $_ -match “Taxprep Forms 2021” } | select UninstallString
if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace “msiexec.exe”,”” -Replace “/I”,”” -Replace “/X”,””
$uninstall64 = $uninstall64.Trim()
Write "Repairing Old install..."
Start-Process "msiexec.exe" -arg "/fa $uninstall64" -Wait
Write “Uninstalling…”
start-process “msiexec.exe” -arg “/X$uninstall64 /qb” -Wait}
if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace “msiexec.exe”,”” -Replace “/I”,”” -Replace “/X”,””
$uninstall32 = $uninstall32.Trim()
Write "Repairing Old install..."
Start-Process "msiexec.exe" -arg "/fa $uninstall32" -Wait
Write “Uninstalling…”
start-process “msiexec.exe” -arg “/X$uninstall32 /qb” -Wait}
I also tried:
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force
$DisplayName = "Taxprep Forms 2021"
$TaxprepCheck = (Get-ItemProperty HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $DisplayName}) -ne $null
$PublicUser = "C:\Users\Public\CCH\Taxprep Forms 2021\"
$UnInstallCheck = "C:\Users\Public\CCH\Taxprep Forms 2021\Forms2021Uninstall.txt"
$Uninstalled = "C:\Logs\TaxprepForms2021.1.0Uninstalled.txt"
$TaxprepVer = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq $DisplayName}
If(-Not $TaxprepCheck)
{
New-Item $Uninstalled -Force -ErrorAction Stop
Set-Content $Uninstalled "Taxprep Forms 2021.1.0 is not installed"
}
else
{
$TaxprepVer.Uninstall()
New-Item $Uninstalled -Force -ErrorAction Stop
Set-Content $Uninstalled "Taxprep Forms 2021.1.0 is successfully Uninstalled"
}
If (Test-Path -Path $PublicUser)
{
New-Item $UnInstallCheck -Force -ErrorAction Stop
Set-Content $UnInstallCheck "Taxprep Forms 2021.1.0 Uninstalled"
}
Else
{
New-Item -ItemType "directory" -Path $PublicUser
New-Item $UnInstallCheck -Force -ErrorAction Stop
Set-Content $UnInstallCheck "Taxprep Forms 2021.1.0 Uninstalled"
}
I have verified that the application is installed. It's in the registry. These scripts work on some computer and not on others. The machines it doesnt work on I have to uninstall the application through add\remove.
Ok so it seemed I may have caused my own issue. When I initially deployed the application in SCCM, I set the User experience to “Install as User”. The switch I used when calling msiexec was All Users = 2. I switched the install to “Install for System and set All Users = “”. I would need to deploy the msi and do a full install rather than an upgrade using the msp. Installing as User may have messed up the registry setting for the initial install.
I am new to the power shell scripting. I am trying to delete all files except one folder and one file. I run this script by using jenkins it showing error called " Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available." And i am trying to run this script in powershell window but it asking Confirmation [Y/N]. I need to run this script by using jenkins please help me.
$Path = "C:\TeamCity\buildAgent2\work"
$exclude = #("*.old", "*directory.map")
Get-ChildItem $Path -Exclude $exclude | Remove-Item -Force -Confirm:$false -ErrorAction Stop| echo Y
You need to add the Recurse parameter to the remove command.
Like that:
Remove-Item -Recurse -Force
guiwhatsthat is correct your code should like this;
$Path = "C:\TeamCity\buildAgent2\work"
$exclude = #("*.old", "*directory.map")
Get-ChildItem $Path -Exclude $exclude | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction Stop
I've started using a powershell script to clean all files that are older than 30 days in the roaming profiles of all our users.
This is the script I use:
$oldTime = [int]30 #30 days
foreach ($path in Get-Content "pathList.txt") {
Write-Host "Trying to delete files older than $oldTime days, in the folder $path" -ForegroundColor Green
Get-ChildItem $path -Recurse -Verbose | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Recurse -Force
}
Here is the Pathlist.txt
\\FS001\RDS_FolderRedirection$\*\Downloads
\\FS001\RDS_FolderRedirection$\*\Downloads\$RECYCLE.BIN
For some reason the script ignores the $RECYCLE.BIN folder.. am I missing something here?
Try the -Force param with Get-ChildItem, the Documentation explains it's use as:
Allows the cmdlet to get items that cannot otherwise not be accessed by the user, such as hidden or system files.
It would be used in your code like this:
Get-ChildItem $path -Recurse -Verbose -Force
I am currently new at PowerShell and I have created a script based on gathered information on the net that will perform a Delete Operation for found files within a folder that have their LastWriteTime less than 1 day.
Currently the script is as follows:
$timeLimit = (Get-Date).AddDays(-1)
$oldBackups = Get-ChildItem -Path $dest -Recurse -Force -Filter "backup_cap_*" |
Where-Object {$_.PSIsContainer -and $_.LastWriteTime -lt $timeLimit}
foreach($backup in $oldBackups)
{
Remove-Item $dest\$backup -Recurse -Force -WhatIf
}
As far as I know the -WhatIf command will output to the console what the command "should" do in real-life scenarios. The problem is that -WhatIf does not output anything and even if I remove it the files are not getting deleted as expected.
The server is Windows 2012 R2 and the command is being runned within PowerShell ISE V3.
When the command will work it will be "translated" into a task that will run each night after another task has finished backing up some stuff.
I did it in the pipe
Get-ChildItem C:\temp | ? { $_.PSIsContainer -and $_.LastWriteTime -lt $timeLimit } | Remove-Item -WhatIf
This worked for me. So you don't have to ttake care of the right path to the file.
other solution
$timeLimit = (Get-Date).AddDays(-1)
Get-ChildItem C:\temp2 -Directory | where LastWriteTime -lt $timeLimit | Remove-Item -Force -Recurse
The original issue was $dest\$backup would assume that each file was in the root folder. But by using the fullname property on $backup, you don't need to statically define the directory.
One other note is that Remove-Item takes arrays of strings, so you also could get rid of the foreach
Here's the fix to your script, without using the pipeline. Note that since I used the where method this requires at least version 4
$timeLimit = (Get-Date).AddDays(-1)
$Backups = Get-ChildItem -Path $dest -Directory -Recurse -Force -Filter "backup_cap_*"
$oldBackups = $backups.where{$_.LastWriteTime -lt $timeLimit}
Remove-Item $oldBackups.fullname -Recurse -Force -WhatIf