Wednesday, February 2, 2022

PowerShell in Daily life

 Hello All,

This post will cover the small scripts that are used by the System Administrators/SCCM Admins/Desktop Administrator in their daily life. I am not a PowerShell expert and have just started learning it. This post is also a kind of repository/reference point for my work. 

Folder Existence and File copy script

Below script achieves the following:
  • Check for the existence of a folder at the provided directory path.
  • If the folder is found - required files are copied to it from the source location (in this example source location is the local folder where the script is running from). We can also add the -Path parameter and provide the actual source location of the file to be copied from.
  • If the folder is not found, the script will create a folder and then copy the file to that folder.

Script: 

$Folder= 'C:\Users\Default\AppData\Local\Microsoft\Windows\WSUS'#Path of  'WSUS' folder - the folder that we are looking for.#
$Con= 'C:\Users\Default\AppData\Local\Microsoft\Windows' #Directory, in which the 'WSUS' folder will be created#

If (Test-Path -path $Folder) {
 copy-item "ABC.ini" -Destination $folder #ABC.ini is the example file - can be replaced by any file. -Path parameter can also be used if file not present in the local directory.#
} else {
New-Item -Path $Con -Name "WSUS" -ItemType "Directory"
copy-item "ABC.ini" -Destination $folder
}

    
         
Before script execution

After script execution


No comments:

Post a Comment