Cleanup folder/file

This commit is contained in:
Shreya 2020-08-04 17:28:32 +05:30
parent 04eb0650cd
commit fe6cd2b076
1 changed files with 19 additions and 4 deletions

View File

@ -2,10 +2,25 @@ param (
[string]$startup_file_path = $profile
)
If (!(Test-Path $startup_file_path)) { # create profile.ps1 file if it doesn't exist already
New-Item -Path $startup_file_path -ItemType "file" -Force
# check if paths exist already
$startup_file_prev_exists = Test-Path $startup_file_path
$startup_file_folder_path = ($startup_file_path -split '\\')[0..(($startup_file_path -split '\\').count -2)] -join '\'
$startup_file_folder_prev_exists = Test-Path $startup_file_folder_path
# carry out pba
If (!($startup_file_prev_exists)) { # create profile.ps1 file if it doesn't exist already
[Void](New-Item -Path $startup_file_path -ItemType "file" -Force)
}
Add-Content $startup_file_path "# Successfully modified $startup_file_path" ; # add line to $Profile
cat $startup_file_path | Select -last 1 ; # print last line of $Profile
$OldProfile = cat $startup_file_path | Select -skiplast 1 ;
Set-Content $startup_file_path -Value $OldProfile ;
$OldProfile = cat $startup_file_path | Select -skiplast 1 ; # get file's original content
Set-Content $startup_file_path -Value $OldProfile ; # restore file's original content
# cleanup
If (!($startup_file_prev_exists)) { # remove file if it didn't exist previously
Remove-Item -Path $startup_file_path -Force ;
}
If (!($startup_file_folder_prev_exists)) { # remove folder if it didn't exist previously
Remove-Item -Path $startup_file_folder_path -Force -Recurse ;
}