?
Solved

Deleting folders with environment variables included in Powershell

Posted on 2014-01-15
5
Medium Priority
?
299 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 1600 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 1600 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 400 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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

765 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