Tuesday, August 18, 2020

Powershell

robocopy with mirror option 

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

**********************************************************************

Pipeline variables to pass output from one command to another we have special variable 
 
  $_ and $PSItem

Need to know about 

select-Object 
Measure-Object 


To remove files other than  *-routing.module.ts


$filenames_list= (Get-childItem -Path "c:\pavan\temp" -Recurse -Exclude "*-routing.module.ts" -file).FullName
$filenames_list.count
for($i=0;$i -lt $filenames_list.count;$i++){
    Remove-Item -Path  $filenames_list[$i] -Recurse
}


To remove the empty directories use the following and this need to be running multiple times. The directory is consider as empty when there is no file or directory so first child items will be deleted and during the next run parent item will be deleted


$emptyfolderList = (gci c:\pavan\temp -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFileSystemInfos().Count -eq 0} | select FullName
for($i=0;$i -lt $emptyfolderList.count;$i++){
    write-host "File " $i " is " $emptyfolderList.fullname[$i]
    Remove-Item -Path  $emptyfolderList.fullname[$i] -Recurse
}

for deleting the empty directory we can review the below link as well
https://superuser.com/questions/321231/how-to-find-empty-directories-in-windows-using-a-powershell-script

https://www.mssqltips.com/sqlservertip/5058/using-powershell-to-work-with-directories-and-files/
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7
https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-files-and-folders?view=powershell-7
https://www.computerperformance.co.uk/ezine/ezine133/


Delete files 
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-7
**********************************************************************
Scope and Execution policies 

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7

Execution policies : Allsigned , ByPass, Default, RemoteSigned, Restricted, Undefined, Unrestricted
Scope                     : MachinePolicy, UserPolicy, CurrentUser, LocalMachine, Process 

To know the current execution policy of localmachine use  the below 
     Get-ExecutionPolicy

To know current execution policy for all scope below command 
   Get-ExecutionPolicy -List

To know the execution policy of a specific scope  use the below 
   Get-ExecutionPolicy - scope currentuser

To the execution policy use the below 
    Set-ExecutionPolicy -ExecutionPolicy <PolicyName>
    Set-ExecutionPolicy -ExecutionPolicy <PolicyName> -Scope <scope>

To remove the execution policy use the Set-ExecutionPolicy and assign Execution policy as undefined

**********************************************************************

To make the changes for Power and sleep (https://ss64.com/nt/powercfg.html)



Reference link : https://www.reddit.com/r/PowerShell/comments/93evng/power_sleep_settings_how_do_i_change_these_in/

Screen
       Powercfg /Change monitor-timeout-ac 60 #when connected to power
       Powercfg /Change monitor-timeout-dc 0  #when running on battery

Sleep
      Powercfg /Change standby-timeout-ac 0  #when running on power
      Powercfg /Change standby-timeout-dc 0 #when running on battery


To know  computer name 
     $env:computername 
          or
     $env:computername | Select-Object
          or
     Invoke-Expression -Command 'hostname' 
     shorter version is 
                 iex hostname


To rename computer with doamin account who has admin access (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-computer?view=powershell-7)
   Rename-Computer -NewName "ProOnWS-002" -DomainCredential protech\ptummala -Restart

To enable execution of remote commands we to start  Windows Remote Management (WS-Management) or use powershell command  "  Enable-PSRemoting -force " 

 




To know the members in Group use the below command 
   Get-LocalGroupMember -Group Administrators    

To add a user to a group on computer use the below command and make sure to run the command in Admin mode 

        Add-LocalGroupMember -Group Administrators -Member protech\hHarikumar

Reference : https://blog.netwrix.com/2018/09/18/how-to-add-delete-and-change-local-users-and-groups-with-powershell/


To navigate to the windows activation use the below command 
    changepk.exe 

************************************************************

To run powershell from another system 

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_troubleshooting?view=powershell-7

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote?view=powershell-7

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_requirements?view=powershell-7

https://hi.service-now.com/kb_view.do?sysparm_article=KB0687786

https://stackoverflow.com/questions/6587426/powershell-remoting-with-ip-address-as-target

Powercfg /Change monitor-timeout-ac 0
Powercfg /Change standby-timeout-ac 0
Get-LocalGroupMember -Group Administrators
Add-LocalGroupMember -Group Administrators -Member protech\hHarikumar
Get-LocalGroupMember -Group Administrators
changepk.exe

***********************************************************

Punctuation is Powershell --> https://itblog.ldlnet.net/wp-content/uploads/2019/01/PSPunctuationWallChart.pdf

Few Symbols in Powershell

@ hash table 
? where-object
| passes result of one command to the next command 
$ indicates a variable
% forEach-Object

https://d12vzecr6ihe4p.cloudfront.net/media/965959/wp-five-simple-symbols-you-should-know-to-unlock-your-powershell-potential.pdf

Powershell scope : https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.1

Powershell variables : https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-7.1


Quotations in Powershell : https://itblog.ldlnet.net/wp-content/uploads/2019/01/PSPunctuationWallChart.pdf

**************************************************************

XCOPY , RoboCopy 

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy
https://stackoverflow.com/questions/33752732/xcopy-still-asking-f-file-d-directory-confirmation
https://www.minitool.com/backup-tips/xcopy-command.html

**************************************************************

https://www.donovanbrown.com/post/PowerShell-I-would-like-you-to-meet-TFS-and-VSTS


find-module *vsteam*
find-module *tfs*
install-module -name VSTeam -scope AllUsers
get-command -Module VSTeam
get-command *tfs*
Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell
Add-PSSnapin Microsoft.TeamFoundation.Powershell


***********************************************

type and Get-content commands are used to view the content of the file . Like less or cat command in unix we have these


No comments:

Post a Comment