Please help with basic PowerShell to a newbie

Joined
Jul 29, 2017
Messages
3
Reaction score
0
I am familiar with dos commands but powershell commands are new to me.

I have been tasked with writing a powershell command script that does two things:

1. Deletes the contents (all subfolders and the contents of the subfolders) under a particular folder
2. remove a shortcut from the desktop

if that can not be done with simple commands, maybe the steps need to be broken down:

1. gets a list of subdirectores under a directory
2. navigates to each of these directories and deltetes the contents
3. remove the subdirectories
4. then remove shortcuts from the desktop.
 
Joined
Jul 29, 2017
Messages
3
Reaction score
0
I am familiar with dos commands but powershell commands are new to me.

I have been tasked with writing a powershell command script that does two things:

1. Deletes the contents (all subfolders and the contents of the subfolders) under a particular folder
2. remove a shortcut from the desktop

if that can not be done with simple commands, maybe the steps need to be broken down:

1. gets a list of subdirectores under a directory
2. navigates to each of these directories and deltetes the contents
3. remove the subdirectories
4. then remove shortcuts from the desktop.
 
Joined
Feb 18, 2016
Messages
2,835
Reaction score
631
If you look a little deeper into Power shell settings there is a list of commands.
Open Powershell, at the cursor type "get-command" (without the " ") it lists a thousand or more commands with an explanation of what each command does when executed. Hope this is what you're looking for.
 

Regedit32

Moderator
Joined
Mar 4, 2016
Messages
3,617
Reaction score
1,139
Hi wm.m.thompson,

The simplest way to do that would be using Recursion.

For example:

Code:
# Set path to directory that stores the
# files and/or subfolders you wish to delete
# In this example, the path is C:\Users\[username of signed-in User]\Desktop\test"
$path = "$env:userprofile\Desktop\test"

Get-ChildItem -Path $path -Recurse | Foreach-Object { Remove-Item -Recurse -path $_.FullName }


# How this works is as follows:
# Get-ChildItem will be assigned the $path you set.  In this case:  C:\Users\[username of signed-in User]\Desktop\test
# -Path $path is the flag and variable that tells it what ChildItem is [the Directory named "test"] and the path to that Directory
# -Recurse is a flag that instructs the continual scanning of ChildItem until the task on other side of pipe "|" is completed
# | is the aforementioned pipe
# Foreach-Object i.e. each file and/or subfolder inside the ChildItem which is "test" folder on Desktop
# { Remove-Item -Recurse -Path $_FullName }
#     The Remove-Item cmdlet (pronounced command-let) means what it says!  It deletes the item if it exists.
#     The -Recurse flag will ask it to continue until no items exist
#     The -Path is set to $_FullName
#         $_FullName is a RESERVED variable that provides two things:
#           (1) Full path to object, and
#           (2) objects' name.

#           e.g C:\Users\[username of signed-in User]\Desktop\test\file.doc
#           e.g C:\Users\[username of signed-in User]\Desktop\test\subfolder\anotherSubfolder\

The result being that the test Directory will remain on the Desktop, but any subfolders or files that were inside the test Directory will be deleted.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top