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

Posted on 2011-10-21
Last Modified: 2012-05-12
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?
Question by:HPatzen
    LVL 5

    Accepted Solution

    remove -NoProfile and try again

    LVL 1

    Assisted Solution

    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.

    LVL 1

    Author Closing Comment

    Neothwin helped me finding right solution.

    Featured Post

    Want to promote your upcoming event?

    Is your company attending an event or exhibiting at a trade show soon? Are you speaking at a conference? Spread the word by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

    Join & Write a Comment

    This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
    This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now