0 votes

I need to delete several user shares when I delete AD accounts (more than just a simple home drive deletion, we also need to delete terminal services profiles, and other things for custom apps). I have created some custom commands that as simple powershell scripts that run the following code

Remove-Item \\server\share\%username% -Force -Confirm:$False

As a part of the AD Account deletion scheduled task, I have set these custom commands to run as a part of the task.

Everything in the account deletion process works, except for these custom commands. I think the problem may have to do with the fact that the account is deleted before these commands are run, therefore the commands don't work because they don't recognize the user account (since it is no longer present).

The adaxes activity history shows a failed status for the cusom command, and the execution log shows "Could not find a part of the path \\server\share\'.".

It's seems like it's leaving the %username% field empty. These custom commands work just fine when I test them by themselves with sample accounts.


by (100 points)

1 Answer

0 votes
by (215k points)
selected by
Best answer


When a Scheduled Task is run on a user, value references contained in all the actions of the Scheduled Task are resolved before performing any actions. However, if a Scheduled Task launches any Custom Commands, value references are resolved for each Custom Command separately before executing the Custom Command. Since Custom Commands are executed after deleting a user, the account does not exist already, and the %username% value reference cannot be resolved.

To remedy the issue, you can execute the PowerShell scripts directly from the Scheduled Task by adding to the Task the Run a program or PowerShell script actions launching the necessary scripts. In this case, the %username% value reference in your scripts will be resolved before the Scheduled Task performs any actions. To insert the necessary Run a program or PowerShell script actions to your Scheduled Task, you may just copy and paste them from your Custom Commands.


Perfect, I'll give that a try and report back! Thanks!

EDIT: Perfect, worked like a charm! Thanks!

Related questions

0 votes
1 answer

I have a PowerShell Script (being run in a Custom Command) that creates a Scheduled Task that runs another Custom Command but I want the resulting Scheduled ... Exclude = $False $scopeItem.SetInfo() $task.ActivityScopeItems.Add($scopeItem) $task.SetInfo() }

asked Apr 1 by Staj (350 points)
0 votes
1 answer

I am working on a custom deprovision task and have the need to delete a directory on a file server (not a profile directory) and cannot find a function in ... like the following path: \\servername\datafiles\hou\UserName\MyDocuments\* Thanks in advance.

asked Feb 9, 2015 by stuarts24 (50 points)
0 votes
1 answer

Hi, I need to delete a Business Unit. Is it correct that deleting a Business Unit will not delete anything from Active Directory?

asked Sep 6, 2011 by abdutal (110 points)
0 votes
1 answer

I would like to change department without a script just yet if possible on multiple accounts. If I cant do this then I will entertain custom script Thanks :)

asked 3 days ago by will17 (310 points)
0 votes
1 answer

When I tried to login to web interface configurator it ask the username to be UPN (username@domain.com). If I just type SAM (username) it gave warning that username is not valid. Can we enable just to use username ?

asked 4 days ago by fachmi (70 points)
2,599 questions
2,338 answers
843,670 users