Folders aren’t hard to create but if you have a complex directory structure that took time to create (and name properly), you probably don’t want to recreate it unless you have to. You can copy a folder structure pretty easily and ignore the files in it but, if you’d just like to delete all the files in the folder while still retaining the complex folder structure that you’ve created, you can do that as well. Between the two methods, you can take your pick. We’re going to explain how you can delete files but keep folders with a simple PowerShell command.
Delete files but keep folders
You do not need admin rights for this unless you are deleting files from the root of the C drive. If you’re deleting them from your user folder, or any other drive, internal or external, normal user rights will be enough.

เปิด PowerShell และเรียกใช้คำสั่งด้านล่าง แทนที่ 'path-to-folder' ด้วยเส้นทางที่สมบูรณ์ไปยังโฟลเดอร์ที่คุณต้องการลบไฟล์ คำสั่งนี้จะลบไฟล์ทั้งหมดออกจากโฟลเดอร์นี้ และไฟล์ใดๆ ที่อยู่ในโฟลเดอร์ย่อยที่อยู่ภายใต้ การดำเนินการลบจะไม่ส่งไฟล์ไปยังถังรีไซเคิล ดังนั้นโปรดสำรองข้อมูลไว้ก่อน
สั่งการ
Get-ChildItem -Path "path-to-folder" - รวม *.* - ไฟล์ - เรียกซ้ำ | foreach { $_.Delete()}
ตัวอย่าง
Get-ChildItem -Path "D:\Desktop\Jan 13 - 17" - รวม *.* - ไฟล์ - เรียกซ้ำ | foreach { $_.Delete()}

คำสั่งนี้สามารถแก้ไขได้เพื่อกำหนดเป้าหมายไฟล์บางประเภทเช่นกัน ในการทำเช่นนั้น คุณต้องเปลี่ยนสิ่งต่อไปนี้
*.*
แทนที่เครื่องหมายดอกจันที่สองด้วยนามสกุลไฟล์สำหรับไฟล์ที่คุณต้องการกำหนดเป้าหมาย ตัวอย่างเช่น หากคุณต้องการลบไฟล์ข้อความทั้งหมดออกจากโฟลเดอร์ คุณจะใช้คำสั่งด้านล่าง
Get-ChildItem -Path "path-to-folder" - รวม * .txt - ไฟล์ - เรียกซ้ำ | foreach { $_.Delete()}
ไฟล์อื่นๆ ทั้งหมดจะไม่ถูกแตะต้อง เช่นเคย ไฟล์จะไม่ถูกส่งไปยังถังรีไซเคิล ดังนั้นสำรองข้อมูลก่อนที่จะลบ คำสั่งนี้ยังสามารถใช้เพื่อลบไฟล์ที่มีชื่อเดียวกันและมีอยู่ในหลาย ๆ โฟลเดอร์ เช่น image.png อีกครั้ง คุณจะแก้ไขบิตด้วยเครื่องหมายดอกจันสองดอก คำสั่งจะมีลักษณะดังนี้
Get-ChildItem -Path "path-to-folder" - รวม image.png - ไฟล์ - เรียกซ้ำ | foreach { $_.Delete()}
มียูทิลิตี้การลบไฟล์ขั้นสูงที่สามารถทำได้ แต่สิ่งที่ดีที่สุดมักจะซับซ้อนในการค้นหา คำสั่งนี้ใช้งานง่ายและมีโอกาสเกิดข้อผิดพลาดน้อยกว่ามาก เนื่องจากไม่มีสิ่งใดส่งผลให้โฟลเดอร์ถูกลบ