forked from Abhisheksinha1506/Collection-of-Useful-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Managing-Files-and-Folders.ps1
46 lines (33 loc) · 1.53 KB
/
Managing-Files-and-Folders.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Set Variable
$Location = "C:\Users\Trainer\PSFolder\"
# C:\Users\Trainer\PSFolder
# C:\Users\Trainer\PSFolder\TextFiles
# C:\Users\Trainer\PSFolder\Users
# C:\Users\Trainer\PSFolder\Data
# C:\Users\Trainer\PSFolder\PSFolderDelete
# C:\Users\Trainer\PSFolder\PSFolderDelete30
# C:\Users\Trainer\PSFolder\PSFolderNew
Get-Command *Item*
# Retrieve items in a folder
Get-ChildItem -Force $Location
# Retrieve items in a folder and all sub-folders
Get-ChildItem -Force $Location -Recurse
# Include specific file formats
Get-ChildItem -Path $Location -Recurse -Include *.xlsx
# Exclude specific formats
Get-ChildItem -Path $Location -Recurse -Exclude *.xlsx
# Get all item and filter by last write time
Get-ChildItem -Path $Location -Recurse | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-10-22')}
# Create Directory and File
New-Item -Path "$($Location)\PSFolderNew" -ItemType Directory
New-Item -Path "$($Location)\PSFolderNew\PSFile.txt" -ItemType File
# Create Text, CReate File and add Text
$document = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.' | Out-File -FilePath "$($Location)PSFolder\PSDocument.txt"
# Removing Items in Folder
Remove-Item -Path "$($Location)\PSFolderDelete" -Recurse
# Copy Files
Copy-Item -Path "$($Location)\Users\Users.xlsx" -Destination "$($Location)\Users\UsersCopy.xlsx"
# Rename Files
Rename-Item -Path "$($Location)\Users\UsersCopy.xlsx" -NewName "UsersCopyCopy.xlsx"
# Rename File Extensions
Get-ChildItem "$($Location)\TextFiles\*.txt" | Rename-Item -NewName { $_.name -Replace '\.txt$','.bak' }