locked
First Step into PowerShell RRS feed

  • Question

  • 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.
    Friday, July 28, 2017 10:30 PM

All replies

  • Hello

    Technet is your friend :)


    Friday, July 28, 2017 11:23 PM
  • 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

    1.

    DOS: rd /q /s "C:\Some Path"

    PowerShell: Remove-Item -Path "C:\Some Path" -Recurse -Force

    2.

    DOS: del "%userprofile%\desktop\some shortcut.lnk"

    Powershell: Remove-Item -Path "$env:USERPROFILE\Desktop\some shortcut.lnk"

    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.

    You should not need to do this. You would need to create a function that recurses through the tree deleting each file and folder.  That's a bit more complicated for this thread (and totally unnecessary), but here's a helpful hint:

    1.

    DOS: dir C:\Path /d

    PowerShell: Get-ChildItem -Path C:\Path -Directory

    I hope this helps.


    -Tony

    Saturday, July 29, 2017 7:57 AM
  • Please read the following for guidance: This forum is for scripting questions rather than script requests

    \_(ツ)_/

    Saturday, July 29, 2017 9:28 AM
  • Please read the following for guidance: This forum is for scripting questions rather than script requests

    \_(ツ)_/

    Sorry, I did not know this.  However, I don't believe this response falls into the "write the script for me" category.  I just provided examples.

    I have, however, done just that on other threads.  I'll delete those posts, and I'll try to be less helpful in the future. :)


    -Tony

    • Edited by Tony MCP Saturday, July 29, 2017 9:46 AM Clarification
    Saturday, July 29, 2017 9:35 AM
  • Please read the following for guidance: This forum is for scripting questions rather than script requests


    \_(ツ)_/

    Sorry, I did not know this.  However, I don't believe this response falls into the "write the script for me" category.  I just provided examples.

    I have, however, done just that on other threads.  I'll delete those posts, and I'll try to be less helpful in the future. :)


    -Tony

    No need to apologize.  The question is very much a request for a complete solution and I wanted to advise the OP that the request should not anticipate that someone would provide a solution.  Thomas provided direction with some useful CmdLets.  The  OP just needs to learn how to use help to apply the rest as the help examples will show how to use them to create a script.

    Learning PowerShell for a non-programmer or a "GUI" only tech can be a bit difficult at first.  Learning the basics in a formal way is critical to learning.

    The actual answer to removing a folder tree is to just use "Remove-Item"  It works the same as the batch "RD".  Both will remove a whole folder tree in one command.  In PowerShell "Remove-Item" will also remove shortcuts.

    The whole task can be done in two lines in either CMD.EXE (DOS to non-techs) and PowerShell.  In Windows removing a folder will always remove all files and subfolders.


    \_(ツ)_/


    • Edited by jrv Saturday, July 29, 2017 10:07 AM
    Saturday, July 29, 2017 10:05 AM