• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 852
  • Last Modified:

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
# 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?
  • 2
2 Solutions
remove -NoProfile and try again

HPatzenAuthor Commented:
Thank you for you answer.

I got it working now. Removing -NoProfile didn't do the trick but helped me find the solutions.

All I had to do is changing the Scheduled Task so that it now starts the powershell.exe with just the script as parameter. Nothing else.

HPatzenAuthor Commented:
Neothwin helped me finding right solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now