Link to home
Start Free TrialLog in
Avatar of HPatzen
HPatzenFlag for Switzerland

asked on

Powershell Script scheduled task not working when choosing "run whether user is logged on or not"

Hello everyone,

I'm kinda stuck with trying to run a powershell script through a batch as a scheduled task.

I create the task which runs the following batch file:

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Unrestricted -File C:\Scripts\DelDesktopIni.ps1

Open in new window


The powershell file looks like this:
# Get all folders under D:\data and write them into $DataFolder
$DataFolder = Get-ChildItem -Path D:\data | where {$_.psIsContainer -eq $true}

# Set counter $i to 0
$i = 0

#Do-While loop
do {
# Get data from $DataFolder on position $i and write it into $SubDataFolder
$SubDataFolder = $DataFolder[$i]
# Get the desktop.ini files and write the path into $IniFilePath
$IniFilePath = Get-ChildItem -Path D:\data\$SubDataFolder\*.* -Force  -Include desktop.ini

# If the desktop.ini file exists remove it
if ($IniFilePath -ne $null){
# Remove the desktop.ini
Remove-Item $IniFilePath -Force
# Write deleted file into the log file
$IniFilePath | Out-File C:\Scripts\log.txt -Append
}

# Add +1 to counter $i
$i++
}
# Do-While loop until folders are cleaned,
While($i -le $DataFolder.Count)

Open in new window


It all works except "Remove-Item". The Log get's generated (which says that there is a desktop.ini file), the task says 0x0 but the desktop.ini file is still there.

Is there a workaround for this problem?
ASKER CERTIFIED SOLUTION
Avatar of neothwin
neothwin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HPatzen

ASKER

Neothwin helped me finding right solution.