Solved

Deleting folders with environment variables included in Powershell

Posted on 2014-01-15
5
269 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 68

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

758 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

20 Experts available now in Live!

Get 1:1 Help Now