Delete files from Windows 2008 file explorer using PowerShell - windows

I'd like to delete log files using a task scheduler from a 2008 Windows server that'll be run using a task scheduler. I've created my own and used online scripts but nothing has worked. After running the the script manually on the server a prompt appears that halts this process (which seems to be the issue). Below is a script that I've run on my pc that worked and the OS is Windows 1903 and it worked. The only discrepancy that I could this of is that the PowerShell versions are different. Could someone provide advice as to why is doesn't delete files on a Windows 2008 OS?
Skeleton Script:
Get-ChildItem –Path "C:\path\to\folder" -Recurse |
Where-Object {($_.LastWriteTime -lt
(Get-Date).AddDays(-60))} | Remove-Item

Aaron,
Change remove-item to remove-item -Confirm:$false -force It will stop asking you to confirm the deleting of files. – Aaron
This solved the issue.

Related

Using Powershell to drive advanced search in Windows Explorer - How to pipe Out-GridView to Windows Explorer?

I have a folder on a remote computer containing security camera video footage. I want to only search the *.mp4 files for those that are created between 2300 and 0600. The code:
$root = "F:\ispy\video\SWVL"
(Get-ChildItem -Path $root) | Where-Object {$_.lastWriteTime.TimeOfDay.Hours -gt 23 -or $_.LastWriteTime.TimeOfDay.Hours -lt 06} | ls | Out-GridView -PassThru
Does this perfectly, and passes the output (file list) to a PowerShell gridview.... BUT, I need the out to show the files in Windows Explorer.
I'm essentially trying to use a PowerShell script as an advanced search filter.
Hoping someone has some ideas. Eventually, I'm planning to use this as a flow -somehow- in power automate and power apps.... but need to crack this first part.
Thanks,
Gregg
AZ
Your use case is not valid. Windows Explorer
You can, in your script, do something like this.. (dropping the call to Out-GridView as it's not needed for your end results)
# find those files
Get-ChildItem -Path 'F:\ispy\video\SWVL' |
Where-Object {
$PSItem.lastWriteTime.TimeOfDay.Hours -gt 23 -or
$PSItem.LastWriteTime.TimeOfDay.Hours -lt 06} |
# copy them to a temp location
Copy-Item -Destination 'SomeTempPath' -Verbose
# open explorer in that location
Invoke-Item -Path 'SomeTempPath'
... then delete that location when you are done.
Windows Explorer-specific search/filtering is only possible in Windows Explorer. So, that means you can only search to get a specific property, then use GUI automation to send that to the Windows Explorer search box.
Otherwise, just skip the script and know this to avoid overcomplicating what you are after.
In Windows Explorer, you can filter the files by date in File Explorer using the date: keyword. You can use this keyword to find files created before, on or after a certain date. You can use the “>” and “<” signs to find files created after or before the given date. “>=” and “<=” also apply here. While you can manually type the date, File Explorer provides a simple calendar that will show up every time you type date: on the search box.
In a script, you'd have to duplicate the aforementioned. Thus capturing the date in your search, opening Windows Explorer and using SendKeys or AutoIT to select the search box and paste the info then sending enter.
Update as per my comment regarding the pop-up calendar. You can do this, in Windows Explorer to filter by date/date ranges
Manually type it in manually, which of course you could GUI automate via SendKeys or AutoIT.
Click the down arrow on any date column.
In the built-in Windows Sandbox on the latest WinOS builds, the popup still works from the Windows Explorer searchbox.
... but not on other host systems.
Update as per our last comments ...
Yet, if you are really trying to send to the Explore serachbox, then this kludge can do it,...
Start-Process -FilePath 'Explorer' 'd:\temp'
Add-Type -AssemblyName System.Windows.Forms
Start-Sleep -Seconds 2
[System.Windows.Forms.SendKeys]::SendWait('+{TAB}'*2)
[System.Windows.Forms.SendKeys]::SendWait('date: 04-Apr-20..11-Jan-21')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{Enter}')
... but warning SendKeys is quirky, timing-wise, etc. Sometimes is works, sometimes it does not.

How to use Powershell to run a exe with a wildcard as the directory keeps changing with each install

I am attempting to run a .exe file for an app which is installed but requires to be activated. This would be a fairly easy process but the directory with .exe changes name slightly with each install. For example a number is added to each folder after the install (Different devices have different numbers) e.g. test1 and then test2. How could I use a wild card to target the folder as it changes?
Example code:
Start-Process -FilePath "\C:\ProgramData\app*/test.exe"
Please note: the app is not real, this is just for display purposes.
Your path seems to be incorrect. Wildcards should work in PowerShell. For example, the below wildcard works for me in PowerShell 5.0:
Try running Start-Process -FilePath "C:\ProgramData\app*\test.exe"

How to upgrade a Visual Studio project from within a GitHub action?

A Visual Studio project can be upgraded from the command line using the devenv.exe command as follows:
devenv.exe SOLUTION_PATH /Upgrade
Where SOLUTION_PATH is a path to a Visual Studio solution (or project) file.
What is the most direct way to perform this step as part of a GitHub action?
What I Have Tried
So far I have failed to find a way to get devenv.exe into the path of the GitHub Action. There does not appear to be a prebuilt action step for this (setup-msbuild step does not make devenv available). Even hardcoding a path such as
MSDEVENV_PATH: ${{'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.com'}}
... then later ...
run: ${{env.MSDEVENV_PATH}} ${{env.SOLUTION_FILE_PATH}} /Upgrade
fails because the path contains spaces, and I can find no way to add quotation marks.
I am aware of a way to find devenv using powershell and extra downloadable packages however this will require writing a PowerShell script, and presumably signing it, and I have no idea whether this will even work in a GitHub Action. Perhaps there is a much simpler approach, hence my question: what is the most direct way to upgrade a solution?
You need a Windows-based runner. vswhere is the tool to get path to various components of the Visual Studio installation and its folder is in the path (source).
run: |
$devenv = & vswhere.exe '-property' productPath
Start-Process -FilePath $devenv -ArgumentList '${{env.SOLUTION_FILE_PATH}} /Upgrade' -Wait
Thanks to #riQQ's partial answer I currently have the following, which waits for the output to be done using both a pipe to Out-Null suggested in this answer and also waits for the generated files to appear. Note that I am using a path to the project not to the solution. (I could not get solution upgrading to work correctly, although I never worked out why.)
run: |
$devenv = & vswhere.exe '-property' productPath
Write-Output "$devenv"
& $devenv "${{env.VCPROJ_FILE_PATH}}" /Upgrade /NoSplash | Out-Null
Write-Output "devenv launched"
while (!(Test-Path "${{env.VCXPROJ_FILE_PATH}}")) { Start-Sleep -Seconds 10 }
Write-Output "vcxproj found"
while (!(Test-Path "${{env.VCXPROJ_FILTERS_FILE_PATH}}")) { Start-Sleep -Seconds 10 }
Write-Output "vcxproj.filters found"
Start-Sleep -Seconds 10
Write-Output "done."
For some reason this step is taking over 5 minutes to complete when run as part of a GitHub Action. It takes only a few seconds on my local machine. Because of the nasty file polling I don't consider this an ideal solution, but I am posting it here for reference.

Powershell Command to copy from network location to all users profile

I'd like a simple command to copy a shortcut for Devices and Printer.lnk to All Users Profile Start Menu so that I can pin this shortcut to every users start menu that logs in to the Windows 10 PC at logon.
I've exported my start menu but having read up on it doesn't seem possible to pin the Devices and Printers shortcut in the normal fashion. This is why I've uploaded the shortcut to a network location that all users have access to, if I can copy this to a location on the PC that all users have access to then I can adjust my startmenu script so that it appears as a tile.
I can copy the shortcut to a basic location (the logged on user) but not to all users folder?
I thought that the following would work but doesn't:
Copy-Item -Path "\\Server\Share\*.lnk" -Destination "$env:allusersprofile\APPDATA\Microsoft\Windows\Start Menu\Programs"
Appreciate your help.
Edit: I have resolved the above issue however, it's lead me on to a follow on issue. I apply this script when a user logs in to Windows 10 but I then want to run a separate task (that is a part of the same ps1 file) which removes a whole host of Bloatware from the OS.
I've tried to add a ; and additionally and but they both still require the user to login twice to complete both tasks. My full script at the moment is:
Copy-Item -Path "\\Server\Share\*.lnk" -Destination "$env:APPDATA\Microsoft\Windows\Start Menu\Programs"; $AppList = #( "*Microsoft.3dbuilder*" etc etc*" ) foreach ($App in $AppList) { Get-AppxPackage -Name $App | Remove-AppxPackage -ErrorAction SilentlyContinue }
I've added "etc etc" as there's a load of commands below that I won't include that remove the bloatware.
You need to remove APPDATA from Destination and it should work. Use following code:
Copy-Item -Path "\\Server\Share\*.lnk" -Destination "$env:ALLUSERSPROFILE\Microsoft\Windows\Start Menu\Programs"

Uninstalling Systrack from CMD

So I'm trying to remotely uninstall the application SysTrack using:
wmic product where "description='Systems Management Agent' " uninstall
but for some reason it can't find the product. Doing a
product get name
from the wmic:root console, I don't see it listed. I'm wondering why the wmic can't get all the list of installed programs? It shows up on programs and features list, but now when I run that wmi command. I am a domain admin so the credentials should be a problem (the folder in the Program Files(x86) folder for SysTrack does have a lock on it though, but I can access)
Side note: I really wish there was a way to remotely just view that programs and features menu. Would be incredibly handy for the tasks I've been doing lately.
try;
wmic product where "name like 'Systems Management Agent'" call uninstall /nointeractive
it should work.
Try this in powershell ise. It will take a list of hostnames from a text file and uninstall the application. Edit the path for your local directory and text file name.
This script is 2 lines. Everything before $app.Uninstall() is on one line and then $app.Uninstall() is the 2nd line.
$app = Get-WmiObject -Class Win32_Product -ComputerName (Get-Content -Path "C:\Users\MYUSERNAME\Documents\PowerShell\servers.txt") | Where-Object {$_.Name -match “Systems Management Agent”}
$app.Uninstall()

Resources