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 (510 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 (1.8k 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 (280 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 (880 points)
0 votes
1 answer

Is there a way to get the name of the user who approved a request and supply that to a step inside of a custom command? For example, HR submits a status change for an employee. ... and pass it as a param in a custom command that is called in one of the steps?

asked 2 days ago by davfount90 (250 points)
0 votes
0 answers

usually we have to set up users account with access to many share mailbox. However, the time consuption adding those emails one by one it is too much as we needs to sep up many users in one day and that task can take from 10 minutes to an hour.

asked Apr 29 by Sandovis (250 points)
2,466 questions
2,215 answers
335,367 users