Solved

Deleting folders with environment variables included in Powershell

Posted on 2014-01-15
5
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 85

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 85

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 70

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
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…
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…

726 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