Friday, May 15, 2020

PowerShell Scripting Tutorials

PowerShell Scripting Tutorials
SN
Purpose
Cmdlets / Command
Description
Show version
$PSVersionTable
Show PowerShell Version
Update package
Get-Help Add-AppPackage
Update packages
Update package
Get-HotFix
Get-HotFix and to install a hot fix as follows
Update package
Get-HotFix -id kb2741530
Update kb2741530
Create folder
New-Item -Path 'D:\temp\Test Folder' -ItemType Directory
Create a folder in D:\Temp\ with name "Test Folder"
Create file
New-Item -Path 'D:\temp\Test Folder\Test File.txt' -ItemType File
Create a file in D:\Temp\Test Folder with name "Test File.txt"
Copy folder
Copy-Item 'D:\temp\Test Folder' 'D:\temp\Test Folder1'
Copy a folder D:\Temp\Test Folder as D:\Temp\Test Folder1
Copy folder
Copy-Item 'D:\temp\Test Folder' -Destination 'D:\temp\Test Folder1'
Copy a folder recursively D:\Temp\Test Folder to D:\Temp\Test Folder1
Copy file
Copy-Item 'D:\temp\Test Folder\Test File.txt' 'D:\temp\Test Folder1\Test File1.txt'
Copy a file D:\Temp\Test Folder\Test File.txt to D:\Temp\Test Folder1
Copy file
Copy-Item -Filter *.txt -Path 'D:\temp\Test Folder' -Recurse -Destination 'D:\temp\Test Folder1'
Copy all text file recursively D:\Temp\Test Folder to D:\Temp\Test Folder1
Delete folder
Remove-Item 'D:\temp\Test Folder1'
Delete a folder D:\Temp\Test Folder1
Delete folder
Remove-Item 'D:\temp\Test Folder' -Recurse
Remove the folder D:\Temp\Test Folder1 recursively. In first example, PowerShell confirms if directory is not empty. In this case, it will simply delete the item.
Delete file
Remove-Item 'D:\temp\Test Folder\test.txt'
Delete a file D:\Temp\Test Folder\Test.txt
Delete file
Remove-Item 'D:\temp\Test Folder' -Recurse
Remove the folder D:\Temp\Test Folder recursively deleting its all files. In first example, PowerShell confirms if directory is not empty. In this case, it will simply delete the item.
Rename folder
Rename-Item D:\temp\Test D:\temp\Test1
Rename a folder D:\Temp\Test to D:\Temp\Test1
Rename file
Rename-Item D:\temp\Test\test.txt test1.txt
Rename a folder D:\Temp\Test\test.txt to test1.txt
Retrieving Item
Get-Content D:\temp\Test\test.txt
Read a file D:\Temp\Test\Test.txt
Retrieving Item Length/size
(Get-Content D:\temp\test\test.txt).length
Read the size of the content of the file read.
Check Folder Existence
Test-Path D:\temp\test
Output :
Test-Path D:\temp\test
True
We're having a folder test in D:\temp directory
Check Folder Existence
Test-Path D:\temp\test2
Output :
Test-Path D:\temp\test2
False
We're not having a folder named test2 in D:\temp directory
Check File Existence
Test-Path D:\temp\test\test.txt
Output :
Test-Path D:\temp\test\test.txt
True
We're having a file test.txt in D:\temp\test directory
Check File Existence
Test-Path D:\temp\test\test2.txt
Output :
Test-Path D:\temp\test\test2.txt
False
We're not having a file named test2.txt in D:\temp\test directory
Get System Date
Get-Date
Output :
Get-Date
Saturday, May 05, 2018 9:58:06 AM
we're using Get-Date to get current date
Get-Date -DisplayHint Date
Output :
Get-Date -DisplayHint Date
Wednesday, April 04, 2018
we're using -DisplayHint to print only Date.
Set system date
set-date -Date (Get-Date).AddDays(1)
Output :
Sunday, May 06, 2018 9:59:16 AM
we're using Set-Date to add one more day to current date
Set system date (Revert)
set-date -Date (Get-Date).AddDays(-1)
Output :
Saturday, May 05, 2018 10:00:37 AM
Now revert back to substract added day to current date.
Get-Date -DisplayHint Time
Output :
10:04:18 AM
we're using -DisplayHint to print only Time.
Set System Time
> $timeToAdd = New-TimeSpan -Minutes 60
> set-date -adjust $timeToAdd
Output :
Saturday, May 05, 2018 11:05:16 AM
we're using Set-Date to adjust to 60 minutes.
Set System Time
> $timeToAdd = New-TimeSpan -Minutes -60
> set-date -adjust $timeToAdd
Output :
Saturday, May 05, 2018 10:08:54 AM
Now revert back to substract added time to current date.

No comments:

Post a Comment

Describe BIOS and UEFI

  BIOS (Basic Input/Output System) and UEFI (Unified Extensible Firmware Interface) are firmware interfaces responsible for initializing har...