0

I am in the middle of trying to create a PowerShell script that will run automatically when my hard drive is full to a certain amount.

The script will read paths from a text document saved to C:\paths2.txt, this will allow me to add or delete paths later.

This is what I have come up with at the bottom if you can take a look. When the script is run using pdq deploy in noninteractive mode I get:

"Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available. At C:\Windows\AdminArsenal\PDQDeployRunner\service-4\exec\Error Handling Wrapper.ps1:58 char:2

I tested it in interactive mode and see that I get a prompt saying:

Confirm The item at Microsoft.PowerShell.Core\FileSystem::C:\LocalSlaveData\O has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

How can I change it so all commands are forced even the children folders like it's asking? I put force in all my commands so I'm not sure why it's giving a prompt.

Thanks for your help in advance


# Check if free space on local disk C is less than 20%
$disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='C:'"
$freeSpaceGB = [math]::Round(($disk.FreeSpace / 1GB), 2)
$percentFree = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)

if ($percentFree -gt 20) {
    Write-Host "Free space on local disk C is greater than 20%. Script will exit."
    Exit
}

# Read the list of paths to delete files from the paths.txt file
$paths = Get-Content "C:\paths2.txt"

# Loop through each path and delete files that are more than 3 days old
$totalDataDeleted = 0
foreach ($path in $paths) {
    $files = Get-ChildItem $path -Recurse -Force | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-3) }
    $dataDeleted = ($files | Measure-Object -Property Length -Sum).Sum
    $totalDataDeleted += $dataDeleted
    $files | Remove-Item -Force
}

# Display the total amount of data deleted in GB
$totalDataDeletedGB = [math]::Round(($totalDataDeleted / 1GB), 2)
Write-Host "$totalDataDeletedGB GB of data has been deleted."
Alan
  • 25
  • 4
  • Is your question about a specific version of PowerShell? If not please remove the according tags. The tags `shell` and `deployment` seem not to fit very well either. ;-) – Olaf Apr 13 '23 at 13:29
  • Hey thanks for getting back, no I don't think it's for a specific version, I put down 2.0 and 3.0 in case someone has a way to make it work in these versions. – Alan Apr 13 '23 at 13:35
  • 1
    But the tags are meant to be used for issues you have specific to these versions of PowerShell. `¯\_(ツ)_/¯` – Olaf Apr 13 '23 at 13:37
  • 2
    Unfortunately, different cmdlets require different mechanisms for suppressing confirmation prompts, namely `-Confirm:$false` vs. `-Force`; in the case of `Remove-Item` specifically, `-Recurse` is needed to avoid a prompt when removing a _nonempty directory_. See the linked duplicate for details. – mklement0 Apr 13 '23 at 13:38
  • 1
    thanks, that solved it, and thank you for also explaining to me what was going on – Alan Apr 13 '23 at 13:59
  • Glad to hear it, Alan. As for tags: I suggest only using a versioned tag if you need a solution for a specific, older PowerShell version, and then use only _one_ such tag, namely for the _oldest_ version you still need to support. Otherwise, just [tag:powershell] will do, complemented by `[tag:powershell-core]` if you're using [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md). Also, the title of your question doesn't quite capture your problem's gist, which is how to avoid an unwanted prompt. – mklement0 Apr 13 '23 at 15:53

0 Answers0