23. februar 2023

Shrink SQL log

To find database file names use below query

select * from sys.database_files; 



USE {database-name};  

GO  
-- Truncate the log by changing the database recovery model to SIMPLE.  
ALTER DATABASE {database-name}
SET RECOVERY SIMPLE;  
GO  
-- Shrink the truncated log file to 1 MB.  
DBCC SHRINKFILE ({database-file-name}, 1);  
GO  
-- Reset the database recovery model.  
ALTER DATABASE {database-name}
SET RECOVERY FULL;  
GO

23. januar 2023

Powershell - Delete files older than "$Days" days

#---- Delete files in $Path older than $Days ----#
$Path = "C:\Temp"
$Days = "30"
dir $Path -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt $Days } | Remove-Item -Force -WhatIf

Recursive delete :
dir $Path -recurse -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt $Days } | Remove-Item -Recurse -Force -WhatIf


Remark : Parameter "-WhatIf" is there only for testing, remove when sure it deletes right files
TEST IN Powershell ISE before deleting wrong files