Solved

Deleting folders with environment variables included in Powershell

Posted on 2014-01-15
5
275 Views
Last Modified: 2014-01-16
I need to delete certain folders when a user logs into their computer using powershell.

I am familiar with remove-item, but not sure how to use that using an environment variable.

I need to delete/wipe the following folders under

c:\users\%username%\my files
c:\users\%username%\my deltas
c:\users\%username%\my games
c:\users\%username%\my contacts

I appreciate the help, will be rewarded right away.

Thanks!
0
Comment
Question by:mystikal1000
5 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 400 total points
ID: 39782547
You want to delete folders under %UserProfile%, not under "c:\users\%username%".
Anyway, try
ForEach ($Folder In "My Files", "My Deltas", "My Games", "My Contacts") {
	$Path = Join-Path ${ENV:UserProfile} $Folder
	"Doing something with '$Path' ..."
}

Open in new window

0
 
LVL 10

Expert Comment

by:remmett70
ID: 39782562
The folders you want to delete are in the environment variable Usersprofile

remove-item $env:usersprofile\<foldername> -confirm:$false -recurse
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39782673
Do I have to be a local admin to run the execution policy?

File H:\Powershell\Delete Folders\deletefolders.ps1 cannot be loaded because the executio
n of scripts is disabled on this system. Please see "get-help about_signing" for more det
ails.
At line:0 char:0
0
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 400 total points
ID: 39782785
Depends. If you want to set it for the system, you need an elevated prompt.
If you want to do it for a regular user (or an administrative user with UAC active in a non-elevated console), you can, for example, use
Set-ExecutionPolicy  -ExecutionPolicy RemoteSigned -Scope CurrentUser

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 39783041
It definitely makes sense to change that on the machine instead for the current user, so you have to set it up only once.
One way to execute the command elevated is:
Start-Process -Verb RunAs powershell -Argumentlist 'set-executionPolicy RemoteSigned'

Open in new window

It will ask for confirmation or admin password.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The viewer will learn how to count occurrences of each item in an array.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

773 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